简体   繁体   中英

JQuery: how to exclude from selection div elements that contain a span element?

I have selected a group of div elements from a table, filtered the selection to only the div elements that contain the class __gwt_cell , and I have saved them into an array, with this command

var myarray = $('table > tbody > tr > td > div').filter('div[__gwt_cell]')

Now I would like to exclude from this selection all the div elements that contain a span element, so all the snippets like this:

<div style="outline-style:none;" __gwt_cell="cell-gwt-uid-615" tabindex="0">
    <span class="gwt-CheckBox">
        <input type="checkbox" class="filled-in" tabindex="-1" value="on" id="gwt-uid-708"> 
        <label for="gwt-uid-708"></label>
    </span>
</div>

In order to do this, I have read this thread , reverted the syntax of the solution, and wrote this command

var myarray = $('table > tbody > tr > td > div').filter('div[__gwt_cell]').filter('div:not(div span)');

that unfortunately does not work.

How can I get the desired result?

the :has() selector is what you need

var myarray = $('table > tbody > tr > td > div').filter('div[__gwt_cell]').not('div:has(span)');

the last piece of the command, that is .not('div:has(span)') , is saying

"exclude div elements that have at least one span element as child "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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