更改 jQuery 中的 aria-controls 属性

Changing aria-controls attribute in jQuery

我很难尝试使用 jQuery 更改 class 的属性。 'count++' 之前的行对 .collapseA 没有影响,但上面的行有效。

$(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">

谢谢

原来我只需要删除 'closest' 位,因为该对象已经引用了它自己。德普

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