css js 改变字体颜色

点击span后字体变成红色,移开后依旧保持红色怎么做。

动漫人物
浏览 2011回答 7
7回答

摇曳的蔷薇

<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <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>&nbsp; &nbsp; // js版&nbsp; &nbsp; var aLi = document.querySelectorAll('.test');&nbsp; &nbsp; for (var i = 0; i < aLi.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; aLi[i].addEventListener('click', function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.style.color='red'&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; // jquery版本&nbsp; &nbsp; $(".test").on('click', function(){&nbsp; &nbsp; &nbsp; &nbsp; $(this).css('color', 'red');&nbsp; &nbsp; });&nbsp; &nbsp;&nbsp;</script></body></html>

一只萌萌小番薯

<p&nbsp;onclick="this.style.color='#f00'">demo</p>

慕丝7291255

<span id="demo">点击变色</span>//js$("#demo").on("click",function(){&nbsp; &nbsp; $("#demo").css("color","red")})&nbsp; &nbsp;&nbsp;

互换的青春

<span id="demo">点击变色</span>//jsdocument.querySelector('#demo').onclick=function(){&nbsp; &nbsp; document.querySelector('#demo').style.color='red'};&nbsp;

慕码人2483693

比较赞同楼上做法:$(span).on("click",function () {$(this).addClass("red")})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript