javascript 按 id 属性的一部分查找所有元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13670821/
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
Find all elements by part of the id attribute
提问by mousesports
I was wondering if this was possible:
我想知道这是否可能:
I have a set of divs, each with an ID ending in '_font', e.g 'body_font', 'heading_font', 'tagline_font', etc.
我有一组 div,每个 div 的 ID 都以“_font”结尾,例如“body_font”、“heading_font”、“tagline_font”等。
Is there a way to grab those elements by searching for a common portion of their names, in this case '_font' so I can later manipulate them using jQuery?
有没有办法通过搜索它们名称的公共部分来获取这些元素,在这种情况下是“_font”,以便我以后可以使用 jQuery 操作它们?
回答by James Allardice
You can use an "attribute ends-with" selector:
您可以使用“属性结束”选择器:
var elems = $("div[id$='_font']");
If you spend some time browsing the jQuery API you should be able to answer questions like this yourself without having to post on Stack Overflow.
如果您花一些时间浏览 jQuery API,您应该能够自己回答这样的问题,而无需在 Stack Overflow 上发帖。
Other selectors that might come in useful:
其他可能有用的选择器:
回答by Adil
回答by Rahul Tripathi
Try this:-
试试这个:-
var elements = $("[id$='_font']");
回答by Gurpreet Singh
Yes it is possible, use:
是的,有可能,请使用:
var elements = $('[id$=_font]');
回答by Kyle Burton
jQuery's Attribute Ends With Selectorshould do what you are asking:
jQuery 的Attribute Ends With Selector应该按照您的要求执行:
$("div[id$='_font']")