<div class="class1">
<div class="item-class1">
<a href="http://www.example.com/1" target="blank" title="title1">
<b>Test1</b>
</a>
</div>
</div>
我确实尝试使用此代码提取链接:
var list = [];
$('.class1 > .item-class1').find('b').each(function (index, element) {
list.push($(element).text());
});
我做错了什么?
答案 0 :(得分:0)
你只是没有使用好的jQuery选择器。您必须使用.attr('href')
方法。
这是一个完整的例子:
<div class="class1">
<div class="item-class1">
<a class="tobelisted" href="http://www.example.com/1" target="_blank" title="title1">
<b>Test1</b>
</a>
</div>
</div>
<div class="class1">
<div class="item-class1">
<a class="tobelisted" href="http://www.example.com/2" target="_blank" title="title1">
<b>Test2</b>
</a>
</div>
</div>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script type="text/javascript">
var list = [];
$('.tobelisted').each(function(){
list.push($(this).attr('href'));
});
console.log(list);
</script>