所以我的HTML看起来像这样:
<div id="setence">
<select id="spellingSuggestions">
<option class="0" value="teh">teh</option>
<option class="1" value="the">the</option>
<option class="2" value="Te">Te</option>
<option class="3" value="tech">tech</option>
<option class="4" value="Th">Th</option>
<option class="5" value="eh">eh</option>
</select>
<select id="spellingSuggestions">
<option class="0" value="wuick">wuick</option>
<option class="1" value="quick">quick</option>
<option class="2" value="wick">wick</option>
<option class="3" value="Vick">Vick</option>
<option class="4" value="Buick">Buick</option>
<option class="5" value="whack">whack</option>
</select>
<span class="correctWord">brown</span>
<select id="spellingSuggestions">
<option class="0" value="fx">fx</option>
<option class="1" value="Fax">Fax</option>
<option class="2" value="Fox">Fox</option>
<option class="3" value="fax">fax</option>
<option class="4" value="fix">fix</option>
<option class="5" value="fox">fox</option>
</select>
<span class="correctWord">jumped</span>
<span class="correctWord">over</span>
<select id="spellingSuggestions">
<option class="0" value="teh">teh</option>
<option class="1" value="the">the</option>
<option class="2" value="Te">Te</option>
<option class="3" value="tech">tech</option>
<option class="4" value="Th">Th</option>
<option class="5" value="eh">eh</option>
</select>
<select id="spellingSuggestions">
<option class="0" value="lzy">lzy</option>
<option class="1" value="lazy">lazy</option>
<option class="2" value="Ly">Ly</option>
<option class="3" value="Lay">Lay</option>
<option class="4" value="Loy">Loy</option>
<option class="5" value="lay">lay</option>
</select>
<span class="correctWord">dog</span>
</div>
我正在尝试按顺序获取每个<option>
中每个跨度和第二个<select>
(。1)的内容/值,并将其存储在变量中。
在这种情况下,我试图获取变量的字符串是“快速棕色传真跳过懒狗”
在伪代码中,我会:
我怎么能用javascript / jQuery做到这一点?
答案 0 :(得分:2)
var str = "";
$('#sentence').children().each(function(){
if($(this).is('.correctWord')){
str += " "+$(this).text();
}else if($(this).is('select')){
str += " "+$(this).find('option').eq(1).val();
}
})
演示--->
http://jsfiddle.net/8juVe/