Say I'm looking for all elements with an attribute 'data-language', whose value begins with 'java' (would match both 'java' and '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:
- all elements that have an attribute name beginning with "data-s", or
- all elements that have data attributes at all (attributes beginning with "data-").
Say I'm looking for all elements with an attribute 'data-language', whose value begins with 'java' (would match both 'java' and '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:
- all elements that have an attribute name beginning with "data-s", or
- all elements that have data attributes at all (attributes beginning with "data-").
-
1
I think we all mis-read this, you want to find all elements that have an attribute beginning with
data-
, so, you want all elements containing a custom data attribute, yes? – tymeJV Commented Sep 23, 2013 at 15:38 - Yes, exactly! Sorry, I can see how it could be misread. – jbyrd Commented Sep 23, 2013 at 15:41
- 2 I figured those bolded words meant something important :) – tymeJV Commented Sep 23, 2013 at 15:41
- haha - ok, I've updated the question - hopefully it's a little clearer now. – jbyrd Commented Sep 23, 2013 at 15:43
-
"I know how to get all elements that have an attribute value beginning with something:
$('[data-size^="value"]')
" That won't do that. – j08691 Commented Sep 23, 2013 at 15:50
2 Answers
Reset to default 9There is no shortcut, you may use the attributes
collection :
$(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.
http://api.jquery./data/
$("div").data() // returns all data attributes to this div
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742294116a4416767.html
评论列表(0条)