62,046
社区成员
发帖
与我相关
我的任务
分享
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title></title>
</head>
<body>
<div id="dongtaiDIV">
</div>
<p>
<input id="addBtn" type="button" value="动态注册代码"
onclick="addControl()" /></p>
<script type="text/javascript">
var AllID = 0;
function addControl() {
document.getElementById("dongtaiDIV").innerHTML += " <input id='btn" + AllID + "' type='button' value='事件按钮" + AllID + "' />";
var Control = document.getElementById('btn' + AllID);
Control.attachEvent("onclick", function() { alert('OK') });
AllID++;
}
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title></title>
</head>
<body>
<div id="dongtaiDIV">
</div>
<p>
<input id="addBtn" type="button" value="动态注册代码"
onclick="addControl()" /></p>
<script type="text/javascript">
var AllID = 0;
function addControl() {
document.getElementById("dongtaiDIV").innerHTML += " <input name='btn' id='btn" + AllID + "' type='button' value='事件按钮" + AllID + "' />";
var els = document.getElementsByName("btn");
for(var i=0,len=els.length;i<len;i++){
els[i].onclick=function(){alert('ok')};
}
AllID++;
}
</script>
</body>
</html>
var AllID = 0;
function addControl() {
document.getElementById("dongtaiDIV").innerHTML += " <input id='btn" + AllID + "' type='button' value='事件按钮" + AllID + "' onclick="tt('"+AllID +"')" />";
AllID++;
}
function tt(btnID){
var Control = document.getElementById('btn' + btnID);
Control.attachEvent("onclick", function() { alert('OK') });
}