我正试图在它的孩子的rel中添加paren。我想要这个
<div id="gallery-1>
<a href="">
<a href="">
</div>
<div id="gallery-2">
<a href="">
<a href="">
</div>像这样:
<div id="gallery-1" class="gallery">
<a href="" rel="gallery-1">
<a href="" rel="gallery-1">
</div>
<div id="gallery-2" "class="gallery">
<a href="" rel="gallery-2">
<a href="" rel="gallery-2">
</div>我的代码:
$('.gallery a').attr('rel', $(this).parent('.gallery').attr('id'));发布于 2015-06-11 20:58:02
您需要将代码放入回调中。否则,它将在原始函数中调用,而不是对集合中的每个元素调用。
$(".gallery > a").attr("rel", function() {
return $(this).parent().attr("id");
});发布于 2015-06-11 20:35:21
试试这个:
$("div").each(function(){
var $this = $(this);
var id = $this.attr("id");
$this.children().attr("rel", id);
});https://stackoverflow.com/questions/30790823
复制相似问题