2016-07-07 17 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