点哪个就让哪个变成蓝色 同时其它的都变成没有颜色呢 这样写为什么不对?
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
#p1{width: 200px;height: 100px;}
#p1 span{display: inline-block;width:50px;height: 30px; }
</style>
<script type="text/javascript">
window.onload=function(){
var oSm=document.getElementsByName('sm');
for(i=0;i<oSm.length;i++){
oSm[i].style.backgroundColor="";
oSm[i].onclick=function(){
this.style.backgroundColor="blue";
}
}
}
</script>
</head>
<body>
<p id="p1">
<span name="sm">aaa</span>
<span name="sm">bbb</span>
</p>
</body>
</html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p id="p1">
</p>
</body>
</html>
因为你点击的时候只执行
this.style.backgroundColor="blue";这个