jQuery获取类名?只有一个课程

时间:2017-05-06 09:20:53

标签: javascript jquery

如何从此DIV获取课程? 我只想上一节课

简单的HTML     

<h4>THE CLASS NAME FROM THIS DIV IS: 'class1 class2'</h4>
<p id="result-class"></p>

简单的jQuery

var classFormDiv = $('.class1').attr('class');

$('#result-class').html('result=> ' + classFormDiv);

请参阅https://jsfiddle.net/FIERMANDT/tbqLcx32/

2 个答案:

答案 0 :(得分:2)

var classFormDiv = $('.class1').attr('class').split(' ')[0]; 试试这个

答案 1 :(得分:1)

与此split() classFormDiv.split(" ")[0]一起使用此[0]将使用firstclassName。从数组中获取类名(请参阅console.log)

&#13;
&#13;
var classFormDiv = $('.class1').attr('class');

$('#result-class').html('result=> ' + classFormDiv.split(" ")[0]);

console.log(classFormDiv.split(" "))
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h4>How to get Class from This DIV? Just one class I want to take</h4>
<div class="class1 class2"></div>

<h4>THE CLASS NAME FROM THIS DIV IS: 'class1 class2'</h4>
<p id="result-class"></p>
&#13;
&#13;
&#13;