簡體   English   中英

使用jQuery從元素中獲取第一個類

[英]Get the first class from an element with jQuery

如何從此HTML元素中僅檢索第一個類p7

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM