jQuery - 具有以特定字符串开头的类的match元素

jQuery  - 具有以特定字符串开头的类的match元素

问题描述:

我有几个链接,如下所示:

I have a few links that look like this:

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

如何将一个函数绑定到所有具有以 rotate - ?

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-"]')








描述:选择
具有指定属性的元素,其中
值以给定的
字符串开头。 p>

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

因此您的代码应为:

$('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
});