How to Get the Number of Classes in a Div Using jQuery
To get the length of classes in a div using jQuery, you can use the '.length' property. Here's an example:
HTML:
<div class='my-class another-class'></div>
jQuery:
var classLength = $('.my-class').attr('class').split(' ').length;
console.log(classLength); // output: 2
In this example, we first select the div using the class selector $('.my-class'). We then use the '.attr('class')' method to get the value of the class attribute of the selected div. We then use the '.split(' ')' method to split the class attribute value into an array, using the space character as the separator. Finally, we use the '.length' property to get the length of the resulting array, which gives us the number of classes in the div.
原文地址: https://www.cveoy.top/t/topic/nxQ3 著作权归作者所有。请勿转载和采集!