js遍历元素并添加属性

<body>
  <
div id="content">    <span>好好学习</span>   <span>天天向上</span>   </div>
</body>
<script type="text/javascript">
$(document).ready(function(){
    $("#content>span").each(function(index,item){
        var str = $(item).text();
        if(str.indexOf("好好学习")!=-1){
            $(item).css("color","red");
        }
    });
})
</script>

猜你喜欢

转载自www.cnblogs.com/mjtabu/p/11759785.html