如图
点击按钮改变文字状态。
<body> <input type="button" name="name" value="哈哈" /> <input type="button" name="name" value="哈哈" /> <input type="button" name="name" value="哈哈" /> <input type="button" name="name" value="哈哈" /> <input type="button" name="name" value="哈哈" /> <input type="button" name="name" value="哈哈" /> <input type="button" name="name" value="哈哈" /> <input type="button" name="name" value="哈哈" /><input type="button" name="name" value="哈哈" /> </body>
<script type="text/javascript"> var haha = function (id) { var arrEle = document.getElementsByName(‘name‘); for (var i = 0; i < arrEle.length; i++) { if (arrEle[i].id != id) arrEle[i].value = "哈哈"; } } var wuwu = function () { this.value = "呜呜"; //哈哈 haha(this.id); } var ID = 0; var arrID = function () { var arrEle = document.getElementsByName(‘name‘); for (var i = 0; i < arrEle.length; i++) { arrEle[i].id = ID++; arrEle[i].onclick = wuwu; } }; arrID(); </script>