我如何使用jQuery从id =整体答案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();
答案 0 :(得分:2)
您的代码将工作,但您的选择器是错误的。这将是......
$(document.getElementById('overall')).find('.rating.selected').text();
请注意rating
和selected
之间的点和空格。
但是,我认为你过于复杂化了......
$('#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>
答案 1 :(得分:1)
$('#overall .rating.selected')。html()