jQuery - 具有以特定字符串开头的类的match元素 [英] jQuery - match element that has a class that starts with a certain string

查看:619
本文介绍了jQuery - 具有以特定字符串开头的类的match元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a few links that look like this:

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

如何将一个函数绑定到所有具有以<$ c $开头的类的元素c> 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
});

这篇关于jQuery - 具有以特定字符串开头的类的match元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆