在我正在使用的html中,存在以下近似结构:
...
<span class="priceValue">
...
<span id="asd...">
12345
</span>
</span>
...
我想获取第二个(嵌套的)第二个* .text()的数字。问题是我不能使用“ id =”属性。
对jQuery代码有任何想法吗?
如果它是第 一个元素span
中的第一个 .priceValue
,则:
var text = $(".priceValue > span").first().text();
var text = $(".priceValue > span").first().text();
snippet.log(text);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="priceValue">
<div>Not me</div>
<span>One</span>
<span>Not me</span>
</div>
<div class="priceValue">
<div>Not me</div>
<span>Two</span>
<span>Not me</span>
</div>
<hr>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
如果您有多个.priceValue
元素,并且每个元素中都需要第一个span的文本,则比较棘手:
var textArray = $(".priceValue").map(function() {
return $(this).children("span").first().text();
}).get();
var textArray = $(".priceValue").map(function() {
return $(this).children("span").first().text();
}).get();
snippet.log(textArray.join(", "));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="priceValue">
<div>Not me</div>
<span>One</span>
<span>Not me</span>
</div>
<div class="priceValue">
<div>Not me</div>
<span>Two</span>
<span>Not me</span>
</div>
<hr>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句