使用jQuery获取类名 [英] Get class name using jQuery
本文介绍了使用jQuery获取类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用jQuery获取类名
I want to get the class name using jQuery
如果它有id
<div class="myclass"></div>
推荐答案
通过其他方式获取元素作为jQuery对象之后它的类,然后
After getting the element as jQuery object via other means than its class, then
var className = $('#sidebar div:eq(14)').attr('class');
应该可以解决问题。对于ID,请使用 .attr('id')
。
should do the trick. For the ID use .attr('id')
.
如果您在事件处理程序或其他jQuery中方法,其中元素是没有包装器的纯DOM节点,您可以使用:
If you are inside an event handler or other jQuery method, where the element is the pure DOM node without wrapper, you can use:
this.className // for classes, and
this.id // for IDs
两者都是标准DOM方法,并且在所有方法中都得到了很好的支持浏览器。
Both are standard DOM methods and well supported in all browsers.
这篇关于使用jQuery获取类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文