document.getElementById("myBtn").onclick = displayDate;
看似很方便,但會導致程式 runtime 變長吧?
----
實作範例錯誤集:
<button onclick=myFunction>Click Me</button>
<span onMoveover="this.style.color='red'">Mouse over me!</span>
還有疑似錯誤(因結果跑得出來,但程式碼與正解不同):
document.getElementById("myBtn").onclick = displayDate;
----
解答:
<button onclick="myFunction()">Click Me</button>
<span onmouseover="this.style.color='red'">Mouse over me!</span>
document.getElementById("myBtn").onclick =
function(){displayDate()};
----
Events Listener 小節有個酷炫的範例,
隨著視窗被調整改變顯示的文字:
<p id="demo"></p>
<script>
window.addEventListener("resize", function(){
document.getElementById("demo").innerHTML = Math.random();
});
</script>
沒有留言:
張貼留言