4
如何从此HTML元素中检索第一个类p7
?从jQuery的元素中获取第一个类
HTML
<div id="myDIV" class="p7 nextclass class_tt">some content</div>
的JavaScript
$('#myDIV').attr('class').first(); //will not work
如何从此HTML元素中检索第一个类p7
?从jQuery的元素中获取第一个类
HTML
<div id="myDIV" class="p7 nextclass class_tt">some content</div>
的JavaScript
$('#myDIV').attr('class').first(); //will not work
$('#myDIV').attr('class').split(' ')[0];
$('#myDIV').attr('class')
返回一个字符串。 split(' ')
分成一个数组,使用' '
作为分隔符。
您可以使用split()做到这一点:
var firstClass = $('#myDIV').attr('class').split(' ')[0];