jquery hasClass,可以给出类名的开头,以获取完整的类名 [英] jquery hasClass, can it be given the beginning of class name, to get the full class name

查看:380
本文介绍了jquery hasClass,可以给出类名的开头,以获取完整的类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做类似这个问题,但它有点不同,所以那里的解决方案对我不起作用。

I'm trying to do something similar to this question, but it's a bit different, so the solution there isn't working for me.

<span class="a-class another-class test-top-left"></span>

我有一个元素(此代码显示 span 但它可能是 div span 或其他任何东西)。此元素的类以 test - 开头( test-top-left test-右上角等)我在以 test - 开头的类上触发了一个click事件,并将点击的对象保存为 var object = this; 。到目前为止简单的东西

I have an element (this code shows a span but it could be div span or anything). This element has a class beginning with test- (test-top-left, test-top-right etc.) I've triggered a click event on classes starting with test- and saved the clicked object as var object = this;. Simple stuff so far.

我现在要做的是获取该类的全名( test-top-left )。我知道它以 test - 开头,但全名是什么。 问题是还有其他类 a-class 另一类 test-top-left 可以使用 hasClass 来获取课程的全名吗?我不想使用 find() filter(),因为其中可能还有其他元素有 class =test -

What I'm trying to do now is get the full name of that class (test-top-left). I know it starts with test- but what's the full name. The thing is that there are other classes a-class another-class and test-top-left. Can hasClass be used to get the full name of the class? I'd prefer not to use find() or filter() just because there may be additional elements within that also have class="test-"

修改:

我现在的代码是,但它给了我所有的类。我需要的是以 test - 开头的单个班级。

The code I have now is, but it gives me ALL the classes. What I need is the single class beginning with test-.

var object = this;
$(object).attr('class'); 

所以现在我循环遍历所有类并分别测试每个类,这似乎很多不必要的代码我希望jQuery有一个聪明的方法来获得立即点击的确切类。

So now I for loop through all the classes and test each one separately, which seems like a lot of unnecessary code. I'm hoping jQuery has a clever way to get the exact class that was clicked right away.

推荐答案

描述



您可以使用jQuerys 属性包含选择器 .attr() .click()方法。


属性包含选择器 - 选择具有指定属性的元素,其值包含给定的子字符串。

Attribute Contains Selector - Selects elements that have the specified attribute with a value containing the a given substring.

.attr() - 获取匹配元素集中第一个元素的属性值。

.attr() - Get the value of an attribute for the first element in the set of matched elements.

.click() - 将事件处理程序绑定到clickJavaScript事件,或在元素上触发该事件。

.click() - Bind an event handler to the "click" JavaScript event, or trigger that event on an element.



样本



html

<span class="anyclass test-hello">Hello World</span>​

jQuery

$("[class*='test']").click(function() {
    var object = $(this);
    alert(object.attr("class").match(/(test-.*?)(?:\s+|$)/)[1])
;});



查看更新的 jsFiddle



更新



如果您不想使用正则表达式你可以这样做。

Check out the updated jsFiddle

Update

If you dont want to use regex you can do this.

$("[class*='test']").click(function() {
    var object = $(this);
    alert("test-" + object.attr("class").split("test-")[1].split("-"))
;});

  • jQuery - Attribute Contains Selector
  • jQuery - .attr()
  • jQuery - .click()
  • jsFiddle Demonstration

这篇关于jquery hasClass,可以给出类名的开头,以获取完整的类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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