2011-04-06 110 views
4

如何从此HTML元素中检索第一个类p7从jQuery的元素中获取第一个类

HTML

<div id="myDIV" class="p7 nextclass class_tt">some content</div> 

的JavaScript

$('#myDIV').attr('class').first(); //will not work 

回答

10
$('#myDIV').attr('class').split(' ')[0]; 

$('#myDIV').attr('class')返回一个字符串。 split(' ')分成一个数组,使用' '作为分隔符。

4

您可以使用split()做到这一点:

var firstClass = $('#myDIV').attr('class').split(' ')[0]; 
相关问题