Open zzly00 opened 6 years ago
<!-- querySelector('.test1 > p') -->
<div class="test1">
<p>123</p>
<p>456</p>
</div>
var test1 = document.querySelector('.test1 > p');
test1.style.color = 'red';
<!-- querySelectorAll('.test3 > p') -->
<div class="test2">
<p>123</p>
<p>456</p>
</div>
var test2 = document.querySelectorAll('.test2 > p');
test2[1].style.color = 'red';
<!-- getElementsByClassName('test') -->
<div class="test">123</div>
<div class="test">123</div>
<div class="test">123</div>
var tests = document.getElementsByClassName('test');
tests[1].style.color = 'red';
<!-- document.createElement('span') -->
<div class="test3">
<p>123</p>
<p>456</p>
</div>
var test3 = document.querySelector('.test3 > p');
var span = document.createElement('span');
span.innerText = 'hello';
test3.appendChild(span);
span.classList.add('yellow');
<!-- .toggle('yellow') -->
<input type="button" id="btn" value="click me" />
document.querySelector('#btn').addEventListener('click', function(){
document.querySelector('.test3 span').classList.toggle('yellow');
})
<!-- 計數器 -->
<h1 class="count">1</h1>
<input type="button" id="minus" value="-1" />
<input type="button" id="plus" value="+1" />
document.querySelector('#minus').addEventListener('click', function(){
var count = document.querySelector('.count').innerText;
document.querySelector('.count').innerText = parseInt(count, 10) - 1;
})
document.querySelector('#plus').addEventListener('click', function(){
document.querySelector('.count').innerText = parseInt(document.querySelector('.count').innerText, 10) + 1;
})
var count = 1;
document.querySelector('#minus').addEventListener('click', function(){
count --;
document.querySelector('.count').innerText = count;
})
document.querySelector('#plus').addEventListener('click', function(){
count ++;
document.querySelector('.count').innerText = count;
})
var count = 1;
document.querySelector('#minus').addEventListener('click', function(){
changeCountText(--count);
})
document.querySelector('#plus').addEventListener('click', function(){
changeCountText(++count);
})
// 變換顏色
function changeCountText(number){
var countElement = document.querySelector('.count');
countElement.innerText = number;
if(number < 0){
countElement.style.color = 'red';
}else{
countElement.style.color = 'black';
}
}
<!-- todo list -->
<ul class="list">
<li>買早餐</li>
<li>買菜</li>
</ul>
<input type="text" class="text" placeholder="請輸入 todo" />
<input type="button" id="add" value="add" />
document.querySelector('#add').addEventListener('click', function(){
var text = document.querySelector('.text');
console.log(text.value)
document.querySelector('.list').innerHTML += '<li>' + text.value + '</li>';
text.value = '';
})
document.querySelector('#add').addEventListener('click', function(){
var text = document.querySelector('.text');
var item = document.createElement('li');
item.innerText = text.value;
document.querySelector('.list').appendChild(item);
text.value = '';
})
document.querySelector('#add').addEventListener('click', function(){
var text = document.querySelector('.text');
var item = document.createElement('li');
item.innerHTML = `
<div>
${text.value}
<span class='close data-text=${text.value}'>x</span>
</div>
`;
document.querySelector('.list').appendChild(item);
text.value = '';
})
// 刪除功能
document.querySelector('.list').addEventListener('click', e => {
for(var i=0; i<e.target.classList.length; i++){
if(e.target.classList[i] === 'close'){
// 方法一
e.target.parentNode.parentNode.outerHTML = '';
// 方法二
document.querySelector('.list').removeChild(e.target.parentNode.parentNode)
}
}
})
<!-- 表單驗證 -->
<form onsubmit="return test()">
name: <input type="text" class="name" />
<input type="submit" />
</form>
function test(){
var value = document.querySelector('.name').value
if(value === 'name'){
return false;
}else{
return true;
}
}
<!-- 表單驗證 -->
<form>
name: <input type="text" class="name" />
<input type="submit" />
</form>
document.querySelector('form').addEventListener('submit', e => {
var value = document.querySelector('.name').value
console.log(e)
if(value === 'name'){
e.preventDefault();
}
return true;
})
DOM
Document Object Model
innerText、innerHTML、outerHTML
JS引入位置
<script>
放在<head>
中,會因為 HTML、CSS還沒載入完成,而出錯<script>
放在</body>
之上<head>
中,但程式寫在document.addEventListener('DOMContentLoaded', function(){}
中參考資料:[MTR01] 程式導師實驗計畫第一期 Lesson 4-1 之 JavaScript、前端-JS和CSS的引入/写入位置应该放在哪里?