How can I get the current class of a div with jQuery?

后端 未结 11 454
挽巷
挽巷 2020-12-03 00:19

Using jQuery, how can I get the current class of a div called div1?

11条回答
  •  庸人自扰
    2020-12-03 00:50

    From now on is better to use the .prop() function instead of the .attr() one.

    Here the jQuery documentation:

    As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method.

    var div1Class = $('#div1').prop('class');
    

提交回复
热议问题