简体   繁体   English

jQuery最接近属性过滤器

[英]jQuery closest with attribute filter

Does jQuery 1.5.1 support attribute selectors in the closest method? jQuery 1.5.1是否支持最接近的方法中的属性选择器?

Given the structure below, el represents the checkbox with the value 513, and I'm trying to reach up and check the ancestor checkbox with the value of 0 with 给定下面的结构, el代表值为513的复选框,我正试图通过值0来检查祖先复选框。

$(el).closest("input[value='0']")[0].checked = true;

but the selector is returning nothing. 但选择器什么也没有返回。

Am I missing something silly? 我错过了一些傻事吗?

在此输入图像描述


EDIT 编辑

Sigh, so the checkbox with value 0 isn't an ancestor to el , just a sibling of the ul ancestor. 叹了口气,因此值为0的复选框不是el的祖先,只是ul祖先的兄弟。 I got it to work with 我得到它与合作

$(el).closest("ul").siblings("input[value='0']")[0].checked = true;

But us there a cleaner way to grab it? 但是我们有更清洁的方法来抓住它吗?

The .closest() method does support attribute selectors the same as any other jQuery method (although I don't know about the specific version of jQuery you mentioned), but the problem here is that .closest() doesn't just look in the general vicinity, it goes up through the ancestors. .closest()方法 支持属性选择器与任何其他jQuery的方法(虽然我不知道你提到的jQuery的特定版本做),但这里的问题是, .closest()不只是在看一般的附近,它通过祖先上升。 You are trying to select an input element that is not an ancestor of the element you are starting with. 您正在尝试选择一个不是您开始使用的元素的祖先的输入元素。 (Given that inputs can't contain other elements they will never be the ancestors of anything...) (鉴于输入不能包含其他元素,它们永远不会成为任何东西的祖先......)

You should be able to do it by selecting the target checkbox's parent li element first: 您应该可以通过首先选择目标复选框的父li元素来执行此操作:

$(el).closest('li:has(input[value="0"])')
     .find('input[value="0"]').prop("checked",true);

Demo: http://jsfiddle.net/Vpzyj/ 演示: http//jsfiddle.net/Vpzyj/

Of course if you were able to add a class to the top level menu items you wouldn't need to mess around with ":has", you could just say: 当然,如果你能够在顶级菜单项中添加一个类,你就不需要乱用“:has”,你可以说:

$(el).closest('li.topMenu').find('input[value="0"]').prop("checked",true);

I believe that you shouldn't need the [0] as .closest only returns the first element found unlike parents() which returns multiple. 我相信你不应该需要[0]因为.closest只返回找到的第一个元素,而不是parent()返回多个元素。

$(el).closest("input[value='0']").prop("checked") = true;

is the way I would prefer to do it. 是我喜欢这样做的方式。

This is assuming you are trying to check the "value 0" checkbox when you check the check the 513 box. 这是假设您在检查513框时检查“值0”复选框。

Edit: Sorry, misread this. 编辑:对不起,误读了这个。 In reality you need to try and get the <\\li> tag and then get the underlying checkbox. 实际上,您需要尝试获取<\\ li>标记,然后获取基础复选框。 Where you are actually looking for the checkbox which is not a direct parent. 您实际上在寻找不是直接父母的复选框。

Edit 2: You realize your edit doesn't work right? 编辑2:您意识到您的编辑不正常吗? Wait... it would... you used siblings not children my mistake. 等等......它会...你使用兄弟姐妹而不是孩子我的错误。

Edit 3: How about using: 编辑3:如何使用:

$(el).parents().get(':eq(2)').children(':first-child');

or are we not assuming the layout is fixed? 或者我们不假设布局是固定的?

检查: http//api.jquery.com/closest/

.closest( selector ) // version added: 1.3

最近检查 DOM树 ,而不是在同一水平上。

Regarding your question: yes, it does (http://api.jquery.com/closest/) 关于你的问题:是的,确实如此(http://api.jquery.com/closest/)

Your code does not work because closest() goes up through the DOM tree and in your case it would have to go up and and down because your checkbox 0 is not an ancestor of checkbox 513. 您的代码不起作用,因为nearest()通过DOM树上升,在您的情况下,它必须上下移动,因为您的复选框0不是复选框513的祖先。

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

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