简体   繁体   English

如何在带有 id 钩子的 div 中查找选择元素,所选元素不能具有类 .select-box 的父元素?

[英]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如何在带有 id 钩子的 div 中查找选择元素,所选元素不得具有类 .select-box 的父级

HTML code HTML代码

<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 jQuery 代码

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

Edit编辑

select element is nested in some more elements. select 元素嵌套在更多元素中。

$('#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直接子元素。

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

Working pen: https://codepen.io/MoMolog/pen/wvBqQep工作笔: https : //codepen.io/MoMolog/pen/wvBqQep

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

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