ruby -run -ehttpd . -p8000
document.body.addEventListener
當發生什麼事件的時候要做什麼
.addEventListener('', callback)
<script>
document.body.addEventListener('click', function(){
document.body.classList.toggle('light')
})
</script>
window.addEventListener('DOMContentLoaded', function(){})
載入後再執行
<script>
window.addEventListener('DOMContentLoaded', function() {
document.getElementById('toggle').addEventListener('click', function() {
document.body.classList.toggle('light');
if (this.textContent === '關') {
this.textContent = '開';
} else {
this.textContent = '關';
}
});
});
</script>
<script>
window.addEventListener('DOMContentLoaded', function(){
document.getElementById('on').addEventListener('click', function(){
document.body.classList.add('light');
})
document.getElementById('off').addEventListener('click', function(){
document.body.classList.remove('light');
})
})
</script>
<script>
window.addEventListener('DOMContentLoaded', function(){
const statusDiv = document.querySelector('.status')
const statusSpan = document.querySelector('.status span')
document.getElementById('on').addEventListener('click', function(){
document.body.classList.add('light')
statusDiv.classList.remove('off')
statusDiv.classList.add('on')
statusSpan.textContent = '開'
})
document.getElementById('off').addEventListener('click', function(){
document.body.classList.remove('light');
statusDiv.classList.remove('on')
statusDiv.classList.add('off')
statusSpan.textContent = '關'
})
})
</script>
Browser Object Model
window.location
window.location
Location {href: "chrome-search://local-ntp/local-ntp.html", ancestorOrigins: DOMStringList, origin: "chrome-search://local-ntp", protocol: "chrome-search:", host: "local-ntp", …}
$0
可以取元素
$0
.src
Document Object Model
HTML在JavaScript的介面
document / window.document
.getElementById('id')
// let's start coding:
window.addEventListener('DOMContentLoaded', function(){
document.getElementById('create-element').addEventListener('click', function(){
//先用一個變數接起來
const timeDiv = document.createElement('div')
debugger
})
})
點到所選取的元素,就會啟動debugger模式
記得要Network -> Disable Cache打勾勾
timeDiv
<div></div>
this
<button id="create-element">.createElement('div.time') to append</button>
timeDiv.className = "time"
"time"
//增加了class
timeDiv
<div class="time"></div>
new Date()
Thu Aug 06 2020 14:31:03 GMT+0800 (台北標準時間)
document.querySelector('.right')
<div class="right">…</div>
a = new Date()
Thu Aug 06 2020 14:32:51 GMT+0800 (台北標準時間)
把取到的變數拿去程式碼裡面改
window.addEventListener('DOMContentLoaded', function(){
document.getElementById('create-element').addEventListener('click', function(){
//先用一個變數接起來
const timeDiv = document.createElement('div')
// debugger
// 去devTool找如何增加class name
timeDiv.className = 'time'
const currentTime = new Date()
timeDiv.textContent = currentTime.toString()
document.querySelector('.right').appendChild(timeDiv)
})
})
beforeend
= afterchild
<!-- beforebegin -->
<p>
<!-- afterbegin -->
foo
<!-- beforeend -->
</p>
<!-- afterend -->
$(document).ready(function(){
})
$('')
選取器
// let's start coding:
$(document).ready(function(){
// running :現在是不是在執行
let cnt = 0, running = false, interval;
// 設定interval是區域變數
// 取得start的按鈕
$('button').click(function(){
// console.log('234424dsf')
if (running){
clearInterval(interval) // 清interval
$('button').text('start') // 按鈕文字
running = false // 改狀態:沒有在執行
} else {
interval = setInterval(function(){
//interval在跑
cnt = cnt + 1
$('#seconds').text(cnt)
}, 1000)
$('button').text('stop') // 按鈕文字
running = true // 改狀態:有在執行
}
})
})