根据子跨度的内容更改类

时间:2016-05-04 16:54:05

标签: jquery

<h3>
   <span class="glyphicon glyphicon-plus first"></span>
   <div class="asseticon ma">
      <div class="stateicon green">
      </div>
   </div>
   TEXT HERE
</h3>

<div class="accordion">

<h3>
   <span class="glyphicon glyphicon-plus second"></span>
   <div class="asseticon ma">
      <div class="stateicon green">
      </div>
   </div>
</h3>

</div>

<h3>
   <span class="glyphicon glyphicon-plus first"></span>
   <div class="asseticon ma">
      <div class="stateicon green">
      </div>
   </div>
   TEXT HERE
</h3>

<div class="accordion">

<h3>
   <span class="glyphicon glyphicon-plus second"></span>
   <div class="asseticon ma">
      <div class="stateicon green">
      </div>
   </div>
</h3>

</div>

<h3>
   <span class="glyphicon glyphicon-plus first"></span>
   <div class="asseticon ma">
      <div class="stateicon green">
      </div>
   </div>
   TEXT HERE
</h3>

<div class="accordion">

<h3>
   <span class="glyphicon glyphicon-plus second"></span>
   <div class="asseticon ma">
      <div class="stateicon green">
      </div>
   </div>
</h3>

</div>

我正在寻找一个jquery函数来检查SECOND的id是否存在,如果存在,则替换其中一个FIRST类。这是我到目前为止所做的,但它没有按预期工作。

请注意,有多个h3,我需要遍历其中的每一个。

if($("span#first").children().length > 0) {
  $(this).removeClass('glyphicon')
         .removeClass('glyphicon-plus')
         .addClass('glyphicon-none');
}

2 个答案:

答案 0 :(得分:0)

这样的事情怎么样:

$("#second").length && $('#first').toggleClass('glyphicon glyphicon-plus glyphicon-none');

答案 1 :(得分:0)

if( $("#second").length )
{
  $("#first").removeClass('glyphicon').removeClass('glyphicon-plus').addClass('glyphicon-none');
}//#second check