最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery selecting and exclude an element inside a div - Stack Overflow

programmeradmin1浏览0评论

I'm trying to get all the next html code and excluding the DIV with ID=fiscal-address-info using jquery. I started using filter method but no lucky.

This is the original snippet code.

<div id="fiscal-info" class="content-fields">
    <div class="content-title"><h2>Datos Fiscales</h2></div>
    <div class="row">...</div>
    <div class="row">...</div>
    <div class="row">...</div>
    <div id="fiscal-address-info">
    <div class="row">...</div>
    <div class="row">...</div>
</div>

What I'd like to get is:

<div id="fiscal-info" class="content-fields">
    <div class="content-title"><h2>Datos Fiscales</h2></div>
    <div class="row">...</div>
    <div class="row">...</div>
    <div class="row">...</div>
    <div class="row">...</div>
    <div class="row">...</div>
</div>

This is what I've tried:

$('#fiscal-info').filter('#fiscal-address-info');

I'm trying to get all the next html code and excluding the DIV with ID=fiscal-address-info using jquery. I started using filter method but no lucky.

This is the original snippet code.

<div id="fiscal-info" class="content-fields">
    <div class="content-title"><h2>Datos Fiscales</h2></div>
    <div class="row">...</div>
    <div class="row">...</div>
    <div class="row">...</div>
    <div id="fiscal-address-info">
    <div class="row">...</div>
    <div class="row">...</div>
</div>

What I'd like to get is:

<div id="fiscal-info" class="content-fields">
    <div class="content-title"><h2>Datos Fiscales</h2></div>
    <div class="row">...</div>
    <div class="row">...</div>
    <div class="row">...</div>
    <div class="row">...</div>
    <div class="row">...</div>
</div>

This is what I've tried:

$('#fiscal-info').filter('#fiscal-address-info');
Share Improve this question edited Jan 27, 2014 at 21:18 ofimbres asked Jan 27, 2014 at 21:07 ofimbresofimbres 311 silver badge5 bronze badges 1
  • You forgot to post the jQuery you tried. – j08691 Commented Jan 27, 2014 at 21:09
Add a ment  | 

4 Answers 4

Reset to default 5

You can use the filter method and using a not selector:

$("#fiscal-info").children().filter(":not(#fiscal-address-info)")

This return you all the fiscal-info children ,except the excluded one.

You can use the .not() method or :not() selector

http://api.jquery./not-selector/

Code based on your example:

$('div#fiscal-info div').not("#fiscal-address-info");

Have a look into this fiddle

 $( "div" ).filter( $( "#fiscal-address-info" ) );

Gets all div elements and filters them for id #fiscal-address-info Hope this helps!

$('#fiscal-info div').filter(function () {
    return this.id == 'fiscal-address-info'
}).remove();

jsFiddle example

发布评论

评论列表(0)

  1. 暂无评论