如何在 jQuery 中隐藏“for”属性具有特定值的标签标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6413379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:49:42  来源:igfitidea点击:

How to hide a label tag in jQuery that has a certain value for the "for" attribute?

jquery

提问by Sam

I want to hide this label tag:

我想隐藏这个标签:

<label for="id_url">URL</label>

Only thing that distinguishes it from the other form elements is the value inside the for attribute. I don't have access to the form's html to add a class or id to it. So how can I hide it?

唯一区别于其他表单元素的是 for 属性中的值。我无权访问表单的 html 以向其添加类或 id。那么我怎样才能隐藏它呢?

回答by Matt Ball

Use the attribute-equals selector.

使用属性等于选择器

$('label[for="id_url"]').hide();

If you need to use a separate variable that contains the target attribute value:

如果需要使用包含目标属性值的单独变量:

var target = 'id_url'; // or whatever
$('label[for="' + target + '"]').hide();

回答by Scott

The attribute Equals selector will match exactly the value of any attribute.

属性等于选择器将与任何属性的值完全匹配。

$('label[for="id_url"]').hide()

http://api.jquery.com/attribute-equals-selector/

http://api.jquery.com/attribute-equals-selector/

You can also use other selectors (starts with, ends with, etc.) if you have many labels and/or can't necessarily search for a perfect match.

如果您有很多标签和/或不一定要搜索完美匹配,您也可以使用其他选择器(开头、结尾等)。

http://api.jquery.com/category/selectors/

http://api.jquery.com/category/selectors/

EDIT: note that, from a performance standpoint, attribute-based selectors are fairly slow. If you have a lot of them, it could potentially cause a pretty noticeable slowdown in performance, especially in IE. If you find yourself needing to do this quite a bit, you may want to look at a larger architectural change (if possible).

编辑:请注意,从性能的角度来看,基于属性的选择器相当慢。如果你有很多,它可能会导致性能明显下降,尤其是在 IE 中。如果您发现自己需要这样做很多,您可能需要查看更大的架构更改(如果可能)。

回答by Alnitak

Use the jQuery attribute selector:

使用 jQuery属性选择器

$('label[for="id_url"]').hide();