简体   繁体   English

选择外部和儿童,没有内部div

[英]select outer and child ,without inner div

<div class="outer">
 <div class="inner">
  <div class="child">

  </div>
 </div>
</div>

I want to select outer and child only. 我只选择外部和子级。 But selecting outer will include inner and child. 但是选择外部将包括内部和子级。 If I used not ie $(".outer , .child").not(".inner") , it will exclude child too. 如果我not使用$(".outer , .child").not(".inner") ,它将也排除child。

ie I want to select outer and child ,without inner . 我要选择外部和外部的孩子

As you can't select it perfectly using a single query, you'd be better taking it out of the DOM (You can't get the results you want as is ) 由于您无法使用单个查询完美地选择它,因此最好将其从DOM中删除(您无法as is获得所需的结果)

//So clone it it from the dom
var outer = $('.outer').clone();
var child = $('child').clone();

// Remove the `Inner` from the cloned version
outer.find('.inner').detach();

// Add the child to the outer.
outer.append(child);

您可以使用:

$('.outer, .outer :not(.inner)')

只需使用.outer,.child

$('.outer,.child')

Use like this: 像这样使用:

$('.outer .child').not('.inner .child');

demo 演示

Or, how about using this: 或者,如何使用这个:

$('.outer > .child')

demo 演示

Or, are you referring like this: 或者,您是这样指的:

$('.outer, .outer .child').not('.inner .child');

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

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