我正在尝试将点击事件动态添加到按钮。问题是在我单击按钮之前触发了该功能这是我的代码..
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function start() {
var p = document.getElementById("myP");
var b = document.getElementById("myB");
b.addEventListener("click", colorIt(p) );
}
function colorIt(ele){
ele.setAttribute("style", "color : red" );
}
window.addEventListener("load", start , false);
</script>
</head>
<body>
<button id="myB"> click me </button>
<p id ="myP"> test </p>
</body>
</html>
Helenr
相关分类