2017-02-15 975 views
0

我很难尝试使用jQuery更改类的属性。 “count ++”之前的行对.collapseA没有任何影响,但上面的行确实有效。在jQuery中更改aria-controls属性

$(document).ready(function(){ 
var count = 1; 
    $('.collapseTag').each(function (index, value) { 
     var newID = "collapse" + count; 
     $(this).attr("id", newID); 
     $(this).closest('.collapseA').attr("href" , "#" + newID); 
     $(this).closest('.collapseA').attr("aria-controls" , newID); 
     count++; 
    }); 
}); 

它的定位是:

<a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="" class="collapseA"> 

感谢

+1

应该工作.... –

+1

似乎工作。 https://jsfiddle.net/d5r2utdL/ –

+0

我会更新代码,但我不认为有什么可以打破它 – ERushforth

回答

0

原来我只是需要,因为对象指的是本身就已经删除“最近”位。 DERP。

$(this).attr("aria-controls" , newID);