2016-07-07 46 views
1

這是我試過,但它是選擇所有a標記,在其他divs我怎樣才能穿越的深度嵌套的div無類屬性​​

$('div:has(> div > div > a[href*="hello/"])').css("color", "blue");但這種選擇每<a>。但我需要<a>的即在深度嵌套<div> <a href="hello/..">

<body> 
<div class="headers"> 
    <div> 
    <a href="welcome/"> welcomes one</a> 
    <a href="welcome/"> welcomes two</a> 
    </div> 
</div>  
<div> 
    <div> 
    <div> 
    <div> 
    <div> 
     <div> 
     <div> 
     </div> 
     </div> 
     <div> 
     <div> 
     </div> 
     <div class="columns"> 
     <div> 
     <div> <a href="hello/one"> once</a></div> 
     <div> <a href="hello/two"> twice</a></div> 
     <div> <a href="hello/three"> thrice</a></div> 
     </div> 
     </div> 
     </div> 
    </div> 
    </div> 
    </div> 
    </div> 
</div> 
+1

爲什麼不只是'$ ('.columns a')'或'$('a [href * =「hello /」]')'??? ....或者你只需​​要最深的'a'元素? – DaniP

回答

-1

這確保了DIV是最後一個div子(深度嵌套):

var start = $('.headers').parents('div').length; 
$('.headers a').each(function(){ 
     if($(this).parents('div:not(.headers)').length-start>5) 
      $(this).css("color", "green"); 
}); 

DEMO

+0

我編輯了答案。這是因爲在選擇器末尾增加了''''。 –

+0

這是你的意思嗎? –

+0

https://jsfiddle.net/dxdL22e2/3/ ...這不適用於最深的元素 – DaniP