简体   繁体   中英

How to find select elements in a div with id hook, the selected elements must not have parent with class .select-box?

How to find select elements in a div with id hook, the selected elements must not have parent with class .select-box

HTML code

<div id="hook">
   .
   .
   some elements here
    .
    .
    <select class="a">
        <option>Full-time</option>
    </select>

    <div id="select-box">
        <select class="a"> <!--  DO NOT SELECT THIS  -->
            <option>Full-time</option>
        </select>
    </div>

    <select class="a">
        <option>Full-time</option>
    </select>
    <select class="a">
        <option>Full-time</option>
    </select>
</div>

jQuery code

$('#hook').find('select').each(function(e) {
    fun($(this));
});

Edit

select element is nested in some more elements.

$('#hook > select')

> means to only select direct descendants of what is selected before it. So only direct children of #hook that are select elements will be selected.

$('#hook :not(.select-box) select').each((i, e) =>
  fun($(e))
)

Working pen: https://codepen.io/MoMolog/pen/wvBqQep

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