jQuery 使用jQuery查找元素ID包含特定文本的页面上的所有元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1206739/
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 10:54:30  来源:igfitidea点击:

Find all elements on a page whose element ID contains a certain text using jQuery

jquerysearchfilterfindwildcard

提问by user48408

I'm trying to find all elements on a page whose element ID contains a certain text. I'll then need to filter the found elements based on whether they are hidden or not. Any help is greatly appreciated.

我正在尝试在元素 ID 包含特定文本的页面上查找所有元素。然后我需要根据它们是否隐藏来过滤找到的元素。任何帮助是极大的赞赏。

回答by karim79

$('*[id*=mytext]:visible').each(function() {
    $(this).doStuff();
});

Note the asterisk '*' at the beginning of the selector matches all elements.

请注意选择器开头的星号“*”匹配所有元素

See the Attribute Contains Selectors, as well as the :visibleand :hiddenselectors.

请参阅属性包含选择器,以及:visible:hidden选择器。

回答by dnxit

If you're finding by Containsthen it'll be like this

如果你是通过Contains找到的,那么它会是这样的

    $("input[id*='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

If you're finding by Starts Withthen it'll be like this

如果你是通过Starts With找到的,那么它会是这样的

    $("input[id^='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

If you're finding by Ends Withthen it'll be like this

如果你通过Ends With找到,那么它会是这样的

     $("input[id$='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

If you want to select elements which id is not a given string

如果要选择id 不是给定字符串的元素

    $("input[id!='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

If you want to select elements which name contains a given word, delimited by spaces

如果要选择名称包含给定单词的元素,以空格分隔

     $("input[name~='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

If you want to select elements which id is equal to a given string or starting with that string followed by a hyphen

如果要选择id 等于给定字符串或以该字符串开头后跟连字符的元素

     $("input[id|='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

回答by port-zero

This selects all DIVs with an ID containing 'foo' and that are visible

这将选择 ID 包含“foo”且可见的所有 DIV

$("div:visible[id*='foo']");

回答by user48408

Thanks to both of you. This worked perfectly for me.

感谢你们俩。这对我来说非常有效。

$("input[type='text'][id*=" + strID + "]:visible").each(function() {
    this.value=strVal;
});