2012-10-15 2 views
1

에 적용부모 DIV id를 얻고 나는이 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의 사업부에서 ID를 얻을에 적용 할 필요가 다만이 같은 "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")); 

누군가 나를 도울 수 있다면 기쁠 것입니다.

감사합니다.

답변

3
$(".gallery").each(function(){ 
    $("a", this).attr("rel", $(this).attr("id")); 
}); 
+0

이것이 내가 필요한 것입니다. 감사합니다! – dee

2

대신을 시도

더 '라는 질문에 this` 문맥
+0

, 당신 말이 맞아 ... 내 대답 – billyonecan

+0

@billyonecan 감사 없다

$('.gallery a').attr('rel', function() { return $(this).closest('div').attr('id'); });​ 

+0

NP 업데이트, 그것이 몇 분 전에 나는 내 자신을 깨달았습니다 : p – billyonecan