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).
精彩评论