开发者

How do I find all elements with a specific 'name' attribute using jQuery

I want to find all the elements on my page that have a 'name' attribute containing 'x' or 'y' and add a certain class to those elements.

So, the logic I'm looking to apply is

If anything has 'x' or 'y' in the name attribute then add开发者_运维知识库 .yepItHasXorY

It seems like a simple thing. I understand how to search based on an ID or Class but not the name attribute...


You can use an attribute-contains selector, like this:

$("[name*=x], [name*=y]").addClass('yepItHasXorY');

Though, I would add an element type, on there or maybe :input to you're only looking at elements you care about (otherwise it's a much more expensive selector).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜