gpt4 book ai didi

jquery - 需要帮助构建 jQuery 查询

转载 作者:行者123 更新时间:2023-12-01 05:04:36 25 4
gpt4 key购买 nike

我在使用 jQuery 查询时遇到问题。

考虑以下(疯狂的)HTML 示例:

<!-- this is the outermost condition -->
<div id="condition">

<!-- this is a tag that I am looking for -->
<input type="text" />

<div id="condition">
<input type="radio" />
</div>

<div>

<div id="condition">
<input type="checkbox" />
</div>

<!-- this is a tag that I am looking for -->
<input type="text" />

</div>
</div>

鉴于上面的示例标记和最外层条件(如顶部所示),我如何获取该条件内的所有输入元素,这些元素也不是内部条件的成员?

我提供了示例,以便您可以了解我希望查询返回哪些标签。

最佳答案

页面上的所有 html 元素都应该有一个唯一的 ID。

也就是说,你可以这样做:

// select the :first #condition
var $first = $("#condition:first");

// use map() to filter out inputs that don't meet our criteria
// and dump them into the inputs Array.
var inputs = $first.find("input").map(function() {

// select parent with #condition ID
var $parent = $(this).parent("#condition");

// if parent == null or parent doesn't have any parents with ID = #condition
// return the input, otherwise null, which removes it from the list
return $parent.length == 0 || $parent.parents("#condition").length == 0
? $(this) : null;
});

您最终得到的是一个输入的Array,这些输入未包装在#condition中或其父级是第一个#condition元素

<小时/>

工作示例:http://jsfiddle.net/hunter/EsYLx/

关于jquery - 需要帮助构建 jQuery 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6929976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com