你要
.onclick = secondFunction
不
.onclick = secondFunction()
后者调用(执行),
secondFunction而前者
secondFunction在
onclick事件传递给要调用的的引用
function start() { var a = document.createElement("a"); a.setAttribute("href", "#"); a.onclick = secondFunction; a.appendChild(document.createTextNode("click me")); document.body.appendChild(a);}function secondFunction() { window.alert("hello!");}start();
a.addEventListener("click", secondFunction);// ORa.addEventListener("click", function(event) { secondFunction(); event.preventDefault();});
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)