如何从此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);
答案 0 :(得分:2)
var classFormDiv = $('.class1').attr('class').split(' ')[0];
试试这个
答案 1 :(得分:1)
与此split()
classFormDiv.split(" ")[0]
一起使用此[0]
将使用firstclassName。从数组中获取类名(请参阅console.log)
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;