jQuery:获取 ID 包含部分字符串的所有孩子

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

jQuery: get all children whose ID contains part of string

jqueryhtml

提问by ojek

I have a set of links:

我有一组链接:

<div id="parent">
   <a id="UserFirst" href="#"></a>
   <a id="UserSecond" href="#"></a>
   <a id="UserThird" href="#"></a>
</div>

I need a quick way of getting (for example) all the achildren of #parent, whose ID contains the letter i.

我需要一种快速获取(例如) 的所有a子代的方法#parent,其 ID 包含字母i

How can I do it?

我该怎么做?

回答by Unknown

Use jquery contains selector:

使用 jquery contains selector

$("#parent").find("a[id*='i']").each(function(){
//do something here
});

DEMO FIDDLE

DEMO FIDDLE

回答by adeneo

Use the attribute contains selector:

使用包含选择器属性

$('#parent a[id*="i"]');

回答by Blender

Use an attribute selector:

使用属性选择器:

a[id*="i"]

Or .filter():

或者.filter()

$('a').filter(function() {
    return this.id.indexOf('a') !== -1;
});