开发者

jQuery - match element that has a class that starts with a certain string

开发者 https://www.devze.com 2023-01-24 14:19 出处:网络
I have a few links that look like this: <a href=\"#\" class=\"somelink rotate-90\"> ... </a>

I have a few links that look like this:

<a href="#" class="somelink rotate-90"> ... </a>

How can开发者_高级运维 I bind a function to all elements that have a class that starts with "rotate-" ?


You can use starts with selector like this:

$('a[class^="rotate-"]')

Description: Selects elements that have the specified attribute with a value beginning exactly with a given string.

So your code should be:

$('a[class^="rotate-"]').click(function(){
  // do stuff
});

Note: If you want to find out elements that contain certain text anywhere in their attributes, you should do this instead:

$('a[class*="rotate-"]').click(function(){
  // do stuff
});
0

精彩评论

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