亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

js創(chuàng)建一個(gè)input數(shù)組并綁定click事件的方法

 更新時(shí)間:2014年06月12日 11:23:38   投稿:whsnow  
這篇文章主要介紹了js創(chuàng)建一個(gè)input數(shù)組并綁定click事件的方法,需要的朋友可以參考下
復(fù)制代碼 代碼如下:

</pre><pre name="code" class="javascript"><html>
<body>
<input type="button" name="input[]" value="按鈕1" /><br />
<input type="button" name="input[]" value="按鈕2" /><br />
<input type="button" name="input[]" value="按鈕3" /><br />

<div id="add"></div>
</body>
</html>

<script type="text/javascript">

// 通過(guò) getElementsByTagName 獲得都有 input 控件
var inputs =document.getElementsByTagName("input");
// 為第0個(gè)button綁定onclick事件,alert一下
inputs[0].onclick = function(){
alert("我測(cè)試一下");
}

// 為每一個(gè)button綁定onclick事件,alert一下
for(var i=0;i<inputs.length;i++){
inputs[i].onclick = function(){
alert("我測(cè)試一下");
}
}

window.onload = function(){
// 定義一個(gè)數(shù)組 arrs
var arrs = new Array();
// 循環(huán)添加
for(var i=0;i<2;i++){
// 循環(huán)添加兩個(gè) input type="button" value="新增"+i
var input = document.createElement("input");
input.type = "button";
input.value = "新增" + i;
// 記得把創(chuàng)建的 input 放入 arrs 中
arrs.push(input);
// 然后把 input 放入 id="add" 的div中
document.getElementById("add").appendChild(input);
}

// 同樣用 [0].onclick 綁定事件,依然沒(méi)有問(wèn)題
arrs[0].onclick=function(){
alert("我又測(cè)試一下");
}

}
</script>

相關(guān)文章

最新評(píng)論