7 回答
TA贡献1793条经验 获得超6个赞
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<span class="test">demo1</span>
<span class="test">demo2</span>
<span class="test">demo3</span>
<span class="test">demo4</span>
<script src="jquery.js"></script>
<script>
// js版
var aLi = document.querySelectorAll('.test');
for (var i = 0; i < aLi.length; i++) {
aLi[i].addEventListener('click', function(){
this.style.color='red'
});
}
// jquery版本
$(".test").on('click', function(){
$(this).css('color', 'red');
});
</script>
</body>
</html>
TA贡献1859条经验 获得超6个赞
<span id="demo">点击变色</span>
//js
$("#demo").on("click",function(){
$("#demo").css("color","red")
})
TA贡献1797条经验 获得超6个赞
<span id="demo">点击变色</span>
//js
document.querySelector('#demo').onclick=function(){
document.querySelector('#demo').style.color='red'
};
添加回答
举报