I want to get the class name using jQuery
(我想使用jQuery获取类名)
And if it has an id
(如果它有id)
<div class="myclass"></div>
ask by X10nD translate from so
I want to get the class name using jQuery
(我想使用jQuery获取类名)
And if it has an id
(如果它有id)
<div class="myclass"></div>
ask by X10nD translate from so
After getting the element as jQuery object via other means than its class, then
(通过其他方式获取元素作为jQuery对象,然后)
var className = $('#sidebar div:eq(14)').attr('class');
should do the trick.
(应该做的伎俩。)
For the ID use.attr('id')
.(对于ID,请使用.attr('id')
。)
If you are inside an event handler or other jQuery method, where the element is the pure DOM node without wrapper, you can use:
(如果您在事件处理程序或其他jQuery方法中,其中元素是没有包装器的纯DOM节点,则可以使用:)
this.className // for classes, and
this.id // for IDs
Both are standard DOM methods and well supported in all browsers.
(两者都是标准的DOM方法,并且在所有浏)