jQuery endsWith for attributes
using jQuery, I'm trying to see if a random id ends 开发者_如何转开发with a certain string using the following pseudo code:
var node = $('#foobar');
if (node.attr('id').endsWith('bar')) {
// do stuff
}
I know for node selection you can go $(id?='bar')
, but I need something that works for $.attr()
.
Any ideas?
If you already have the desired object and therefore also have the id string as in your example, then this is just a question about matching something at the end of a string so you could just match the id string vs. a regular expression:
if (node.attr('id').search(/bar$/) != -1) {
}
Try this
if (node.filter("[id$='bar']").length > 0){
//do stuff
}
<div id="foo-bar">asd</div>
if ($("div[id$='bar']")) {
// do stuff
}
http://jsfiddle.net/PsaHQ/1/
精彩评论