js创建一个input数组并绑定click事件的方法
作者:whsnow 时间:2023-08-15 02:35:09
</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">
// 通过 getElementsByTagName 获得都有 input 控件
var inputs =document.getElementsByTagName("input");
// 为第0个button绑定onclick事件,alert一下
inputs[0].onclick = function(){
alert("我测试一下");
}
// 为每一个button绑定onclick事件,alert一下
for(var i=0;i<inputs.length;i++){
inputs[i].onclick = function(){
alert("我测试一下");
}
}
window.onload = function(){
// 定义一个数组 arrs
var arrs = new Array();
// 循环添加
for(var i=0;i<2;i++){
// 循环添加两个 input type="button" value="新增"+i
var input = document.createElement("input");
input.type = "button";
input.value = "新增" + i;
// 记得把创建的 input 放入 arrs 中
arrs.push(input);
// 然后把 input 放入 id="add" 的div中
document.getElementById("add").appendChild(input);
}
// 同样用 [0].onclick 绑定事件,依然没有问题
arrs[0].onclick=function(){
alert("我又测试一下");
}
}
</script>
标签:input数组,click事件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 实现端口扫描工具
2022-12-05 23:02:45
微信小程序跳转到其他网页(外部链接)的实现方法
2024-04-17 09:56:17
![](https://img.aspxhome.com/file/2023/1/136461_0s.png)
解决python3爬虫无法显示中文的问题
2022-11-27 15:18:59
Golang使用Consul详解
2024-04-26 17:35:56
python如何建立全零数组
2023-08-02 09:50:45
mysql installer community 5.7.16安装详细教程
2024-01-17 04:55:04
![](https://img.aspxhome.com/file/2023/1/115571_0s.png)
Python中input()函数的用法实例小结
2021-09-04 18:42:59
![](https://img.aspxhome.com/file/2023/8/78598_0s.png)
asp金额大小写转换完全无错版
2007-09-26 09:38:00
go 下载非标准库包(部份包被墙了)到本地使用的方法
2024-05-10 13:56:50
Python取出字典中的值的实现
2022-01-09 20:16:07
python3 cmp实现方式
2023-12-13 18:10:25
Python使用tkinter模块实现GUI界面的学生信息管理系统流程分步详解
2022-04-06 18:09:53
![](https://img.aspxhome.com/file/2023/1/103541_0s.png)
用WEB(ASP)方式实现SQL SERVER 数据库的备份和恢复
2010-05-11 20:12:00
Go语言中日期包(time包)的具体使用
2024-05-08 10:52:33
![](https://img.aspxhome.com/file/2023/1/127361_0s.jpg)
python fire库的使用实例教程
2023-08-24 14:01:22
![](https://img.aspxhome.com/file/2023/0/61760_0s.png)
使用VSCODE配置GO语言开发环境的完整步骤
2024-04-27 15:27:51
Python如何批量生成和调用变量
2023-11-27 21:21:08
960网格系统
2009-02-17 12:22:00
![](https://img.aspxhome.com/file/UploadPic/20092/17/960-grid-system-092-54s.jpg)
HTML和SEO基础知识:H标签全透视
2010-09-21 16:45:00
![](https://img.aspxhome.com/file/UploadPic/20109/21/1412060-45s.jpg)
Spring Security 将用户数据存入数据库
2024-01-13 03:31:46
![](https://img.aspxhome.com/file/2023/0/125600_0s.png)