Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
227 views
Welcome To Ask or Share your Answers For Others

1 Answer

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方法,并且在所有浏)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...