有趣的onclick事件的设立(请关注三楼说法) by yt

不解释,请看代码...

留着讨论...

代码
<html>
<head>
<title>test</title>
<script type="text/javascript" language="javascript">
function window_onload()
{
/////////////
//第一种方式
for(var i = 0 ;i < 5 ;i++)
{
var inputElement
= document.createElement("input");
inputElement.type
= "button";
inputElement.value
= "button"+i;
inputElement.onclick
= function(){
alert(i);
}
document.body.appendChild(inputElement);
}
////////////
//第二种方式
// for(var i = 0 ;i < 5 ;i++)
// {
// var inputElement = document.createElement("input");
// inputElement.type = "button";
// inputElement.value = "button"+i;
//
// setonclick(inputElement, i);
// document.body.appendChild(inputElement);
// }
}
function setonclick(_inputElement, _i)
{
_inputElement.onclick
= function(){
alert(_i);
}

}
</script>
</head>
<body onload="window_onload()">
<div>请分别用代码中提供的两种方式给出当按下某一个 button 的时候 alert出来的值</div>

</body>
</html>

 

posted on 2010-09-20 23:26  gdutlab624  阅读(267)  评论(4编辑  收藏  举报

导航