技术碎片
技术记录
帮助交流
JS点击上色代码
<!doctype html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<title>点击换色</title>
<style>
h1{
cursor:pointer;
}
</style>
</head>
<body>
<h1>00001</h1>
<h1>00002</h1>
<h1>00003</h1>
<h1>00004</h1>
<h1>00005</h1>
<h1>00006</h1>
<h1>00007</h1>
<h1>00008</h1>
<h1>00009</h1>
<h1>00010</h1>
</body>
<script>
h1=document.getElementsByTagName('h1');
for(i=0;i<h1.length;i++){
h1[i].setAttribute('num',0);
h1[i].onclick=function(){
num=parseInt(this.getAttribute('num'));
if(num%2==0){
this.style.background="#ccc";
}else{
this.style.background="#fff";
}
this.setAttribute('num',num+1)
}
}
</script>
</html>
创建html文件复制代码保存即可
如有疑问或者交流问题请加:QQ 2137087126
转载请注明来源地址:www.zhyunxuan.com>
更新时间 2023-02-01
 2053
梦想有多远,路就有多远