[英]jQuery - select all elements with attribute name (not value) beginning with…?
Say I'm looking for all elements with an attribute 'data-language', whose value begins with 'java' (would match both 'java' and 'javascript'). 假设我正在寻找具有属性“数据语言”的所有元素,其值以“ java”开头(将同时匹配“ java”和“ javascript”)。 I know how to do this:
我知道该怎么做:
$('[data-language^="java"]')
But my question is: how do I get all elements that have an attribute ( not the value of the attribute, but the actual attribute name itself ) beginning with something? 但是我的问题是:如何获取具有某个属性( 不是属性的值,而是实际的属性名称本身 )的所有元素,且从某些东西开始? For example:
例如:
There is no shortcut, you may use the attributes
collection : 没有捷径,您可以使用
attributes
集合:
$(someselector).filter(function(){
var attrs = this.attributes;
for (var i=0; i<attrs.length; i++) {
if (attrs[i].name.indexOf("someStartOfName")==0) return true;
}
return false;
});
jQuery has a nice function for this. jQuery为此提供了一个不错的功能。
http://api.jquery.com/data/ http://api.jquery.com/data/
$("div").data() // returns all data attributes to this div
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.