jquery:查找 id 具有特定模式的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1487792/
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
jquery: find element whose id has a particular pattern
提问by deostroll
I am trying to find a span element who has an id in a particular pattern. Its main use is to find certain elements rendered by an asp.net (aspx) page which is derived from a master page.
我试图找到一个具有特定模式 id 的 span 元素。它的主要用途是查找由从母版页派生的 asp.net (aspx) 页面呈现的某些元素。
采纳答案by xxxxxxx
$('span').each(function(){
if( $(this).attr('id').match(/pattern/) ) {
// your code goes here
}
});
problem solved.
问题解决了。
回答by cdmckay
Building on the accepted answer:
基于公认的答案:
It depends on what kind of pattern you're looking for. If your pattern is something like "MasterPageElement_CheckBox_4443", "MasterPageElement_CheckBox_4448", etc. then you could also use:
这取决于你正在寻找什么样的模式。如果您的模式类似于“MasterPageElement_CheckBox_4443”、“MasterPageElement_CheckBox_4448”等,那么您还可以使用:
$("span[id^=MasterPageElement_CheckBox]")
There are 3 built-in attribute selectors for simple patterns:
有 3 个用于简单模式的内置属性选择器:
$("span[id^=foo]")
That selector matches all spans that have an id
attribute and it starts with foo
(e.g. fooblah
)
该选择器匹配所有具有id
属性并以foo
(例如fooblah
)开头的跨度
$("span[id$=foo]")
That selector matches all spans that have an id
attribute and it ends with foo
(e.g. blahfoo
).
该选择器匹配所有具有id
属性并以foo
(例如blahfoo
)结尾的跨度。
$("span[id*=foo]")
That selector matches all spans that have an id
attribute and it has foo
somewhere within in it (e.g. blahfooblah
).
该选择器匹配所有具有id
属性的跨度,并且它在其中的foo
某处(例如blahfooblah
)。