简体   繁体   English

选择不在另一个div jQuery内的div

[英]select a div which is not inside another div jquery

let's say I have an HTML structure like this: 假设我有这样的HTML结构:

<div class="first">
    <div class="sub-1">
        <div class="first again"></div>
    </div>
</div>

How do I select only the div that has the class "first" but which is NOT inside the div that has the class "sub-1". 如何只选择具有“ first”类的div但不在具有“ sub-1”类的div内。 In order words, how do I get only the outer div but extract any div inside that outter div that have the same class than the outter div (I want to get just the div with class="first", not the one with class="first again"). 用命令的话,我如何只获取外部div,但提取该外部div中与外部div具有相同类的div(我只想获取class =“ first”的div,而不是class = “再次”。

Thank you 谢谢

I dont know if you mean a very generic way to handle this but in this particular case you can only write. 我不知道您是不是用一种通用的方式来处理此问题,但在这种情况下您只能编写。

$(".first:first")

A more generic way would be 一种更通用的方式是

$('.first').not('.sub-1 .first').prepend("I was first");

http://jsfiddle.net/JYLVc/ http://jsfiddle.net/JYLVc/

See jQuery documentation for .not() . 参见jQuery文档.not() This should work: 这应该工作:

$('.first').not('.sub-1 .first');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM