<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script> window.onload = function (){ var aBtn = document.getElementsByTagName(‘input‘); var aP = document.getElementsByTagName(‘p‘); // 想建立“匹配”“对应”关系,就用索引值 var arr = [ ‘小云‘, ‘小李‘, ‘小妞‘ ]; for( var i=0; i<aBtn.length; i++ ){ aBtn[i].index = i; // 自定义属性(索引值) aBtn[i].onclick = function (){ // alert( arr[ this.index ] ); this.value = arr[ this.index ]; aP[ this.index ].innerHTML = arr[ this.index ]; }; } }; </script> </head> <body> <input type="button" value="btn1" /> <input type="button" value="btn2" /> <input type="button" value="btn3" /> <p>a</p> <p>b</p> <p>c</p> </body> </html>
示例代码
时间: 2024-10-31 15:16:44