废话不多说代码如下:
方法一:
<em>v</em>ar pArys=document.getElementsByClassName("ps"); for(var i = 1; i <= pArys.length; i++){ //alert(i) var pAry = document.getElementById("p"+i); pAry.onclick=function(){ this.style.fontSize="39px"; } }
方法二:
var pArys=document.getElementsByClassName("ps"); for(var i = 1; i <= pArys.length; i++){ //alert(i) var pAry = document.getElementById("p"+i); pAry.onclick=function(){ this.style.fontSize="39px"; } }
html代码:
<body> <p id="p1" class="ps">事件1</p> <p id="p2" class="ps">事件2</p> <p id="p3" class="ps">事件3</p> <p id="p4" class="ps">事件4</p> </body>
时间: 2024-10-10 09:58:21