jQuery className出现问题 [英] jQuery className woes

查看:113
本文介绍了jQuery className出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取与已检查输入的正则表达式匹配的类的名称.

I'm trying to get the name of a class that matched a regex of a checked input.

如果我有这个:

 <input type="radio" class="toggle toggle-1" />
 <input type="radio" class="toggle toggle-2" checked="checked" />
 <input type="radio" class="toggle toggle-3" />

我想找出'toggle-2'是选中的类.

I want to find out that 'toggle-2' is the checked class.

 $('.toggle:checked').className.match(/toggle\-.+?\b/);

但是产生的"className is undefined"错误.

But that produced 'className is undefined' errors.

我认为我的问题是我针对jQuery对象使用了className.但是我不太确定替代方案是什么.

I think my problem is that I'm using className against a jQuery object. But I'm not really sure what the alternative is.

推荐答案

您可以调用attr方法来获取所需元素上的任何基础属性.

You can call the attr method to get any underlying attributes on the element you need.

$('.toggle:checked').attr('class').match(/toggle\-.+?\b/); 

这篇关于jQuery className出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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