1

このHTMLコードを取得しました:

<div class="gallery" id="gallery_x"> 
    <p>
        <a href="http://a-dynamic-link.com">image1</a>
        <a href="http://another-dynamic-link-com">image2</a>
    </p>
</div>

(x = 変数番号)

.gallery div から ID を取得し、次のように rel として "a" に適用する必要があります。

<div class="gallery" id="gallery_x">
    <p>
        <a rel="gallery_x" href="http://a-dynamic-link.com">image1</a>
        <a rel="gallery_x" href="http://another-dynamic-link-com">image2</a>
    </p>
</div>

これまでの私の(動作していない)ソリューションは次のとおりです。

$(".gallery a").parents(".gallery").attr("rel", $(this).attr("id"));

誰かが私を助けてくれたらうれしいです。

ありがとうございました。

ディー

4

2 に答える 2

3
$(".gallery").each(function(){
    $("a", this).attr("rel", $(this).attr("id"));
});
于 2012-10-15T13:59:48.447 に答える
2

代わりにこれを試してください:

$('.gallery a').attr('rel', function() {
    return $(this).closest('div').attr('id');
});​
于 2012-10-15T13:59:12.793 に答える