jQuery select 最近的子元素 class
jQuery select closest child element by class
我如何使用 jQuery 从 id=overall 答案 div 中选择的评级 div 获取文本?
我想从整个父 div 中的那个 div 动态获取文本 "TESTING"。
<div class='answer' id="overall">
<div class='rating'>1</div>
<div class='rating'>2</div>
<div class='rating'>3</div>
<div class='rating'>4</div>
<div class='rating selected'>TESTING</div>
</div>
<div class='answer' id="effort">
<div class='rating'>1</div>
<div class='rating'>2</div>
<div class='rating'>3</div>
<div class='rating selected'>4</div>
<div class='rating'>TESTING</div>
</div>
我试过这样做,但它是空白的。
$(document.getElementById('overall')).find('.rating selected').text();
您的代码 可以 工作,但您的选择器是错误的。会是...
$(document.getElementById('overall')).find('.rating.selected').text();
注意 rating
和 selected
之间的点和没有 space。
但是,我认为你把事情复杂化了...
$('#overall .selected').text();
示例...
alert($('#overall .selected').text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class='answer' id="overall">
<div class='rating'>1</div>
<div class='rating'>2</div>
<div class='rating'>3</div>
<div class='rating'>4</div>
<div class='rating selected'>TESTING - selected</div>
</div>
<div class='answer' id="effort">
<div class='rating'>1</div>
<div class='rating'>2</div>
<div class='rating'>3</div>
<div class='rating selected'>4</div>
<div class='rating'>TESTING - not selected</div>
</div>
$('#overall .rating.selected').html()
我如何使用 jQuery 从 id=overall 答案 div 中选择的评级 div 获取文本?
我想从整个父 div 中的那个 div 动态获取文本 "TESTING"。
<div class='answer' id="overall">
<div class='rating'>1</div>
<div class='rating'>2</div>
<div class='rating'>3</div>
<div class='rating'>4</div>
<div class='rating selected'>TESTING</div>
</div>
<div class='answer' id="effort">
<div class='rating'>1</div>
<div class='rating'>2</div>
<div class='rating'>3</div>
<div class='rating selected'>4</div>
<div class='rating'>TESTING</div>
</div>
我试过这样做,但它是空白的。
$(document.getElementById('overall')).find('.rating selected').text();
您的代码 可以 工作,但您的选择器是错误的。会是...
$(document.getElementById('overall')).find('.rating.selected').text();
注意 rating
和 selected
之间的点和没有 space。
但是,我认为你把事情复杂化了...
$('#overall .selected').text();
示例...
alert($('#overall .selected').text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class='answer' id="overall">
<div class='rating'>1</div>
<div class='rating'>2</div>
<div class='rating'>3</div>
<div class='rating'>4</div>
<div class='rating selected'>TESTING - selected</div>
</div>
<div class='answer' id="effort">
<div class='rating'>1</div>
<div class='rating'>2</div>
<div class='rating'>3</div>
<div class='rating selected'>4</div>
<div class='rating'>TESTING - not selected</div>
</div>
$('#overall .rating.selected').html()