开发者

How do you use jQuery filter() on an attribute that is not a class or id

I want to filter based on an attribute called "level".

Where I have written -- something here -- I don't kno开发者_Go百科w what to do to reference the level attribute. If it was an id attribute I would do #idName if it was a class I would do .className.

I am not sure what to do to select the level attribute.

$(".myClass").filter(--something here to reference the level attribute --).remove();


filter("[level='2']")


No need for filter, just use the attribute filter syntax, in this case, the Has Attribute Selector:

$(".myClass[level]").remove();

That should remove all .myClass elements that have a non-empty level, of course, you could for instance match level's based on one of the several available operators (see the docs), such as startsWith:

$(".myClass[level^=foo]").remove(); // remove the ones that start with 'foo'

contains:

$(".myClass[level*=haa]").remove(); // remove the ones that contain 'haa'

etc.


If you need to do something complicated, you can write a filter function that returns true for the elements that should be in the filtered set.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜