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];