我是JavaScript的初学者。我在学习时一直在测试我的代码,但是在尝试使用程序化事件处理时,我遇到了一些问题。出于某种原因,以下代码段似乎不起作用。请帮忙。
<!DOCTYPE html>
<title>DOM Interactions</title>
<script>
var b = document.getElementById("btn");
b.addEventListener("click", handler);
function handler(){
var x = document.getElementById("TextChange");
x.innerHTML = "Changed text with programmatic event handling!";
}
</script>
<body>
<p id="TextChange">This will change!</p>
<button id="btn">Click!</button>
</body>
答案 0 :(得分:0)
http://jsbin.com/ninuzeqa/1/edit
尚未阅读的DOM。只需将<script>
放在结束</body>
标记之前。
<script>
var b = document.getElementById("btn");
b.addEventListener("click", handler);
function handler(){
var x = document.getElementById("TextChange");
x.innerHTML = "Changed text with programmatic event handling!";
}
</script>
</body>