这是我的代码:
var s = '<a class="chosen-single chosen-default">\
<span>value1</span>\
<div><b>wsefw</b></div>\
<span>value2</span>\
link\
</a>';
console.log(s.find(".chosen-single > span:first-child").text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我想要的只是value1
。所以我想得到<span>
中第一个a.chosen-single
的值。我怎么能这样做?
答案 0 :(得分:1)
将其转换为DOM,然后:
var s = $('<a class="chosen-single chosen-default">\
<span>value1</span>\
<div><b>wsefw</b></div>\
<span>value2</span>\
link\
</a>');
console.log(s.find('span').eq(0).text()); // or just span:first-child
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 1 :(得分:0)
我认为您正在寻找这样的选择器:
$(".chosen-single > span:first").text()