如何使用jquery获取href属性?

如何使用jquery获取href属性?,jquery,Jquery,但我无法通过此方法获取href。您可以大大简化代码: $('table.table1').each(function(k, elem) { $(this).find('tr').each(function(j, elem){ $(this).find('td').each(function(i,elem){ console.log($(this).fin

但我无法通过此方法获取href。

您可以大大简化代码:

$('table.table1').each(function(k, elem) {
                    $(this).find('tr').each(function(j, elem){
                        $(this).find('td').each(function(i,elem){
                            console.log($(this).find('strong a').attr('href'));
                        })
                    })
                });
$('.table1 strong a').each(function() {
    console.log(this.href);
});

您可以使用
map
方法并避免嵌套
$。每个
语句

$('table.table1 tr td a').each(function(idx, elem) {
    console.log($(this).attr('href'));
});

使用查找方法(效率更高)


您可以向a标记添加一个类,如下所示:

$('.table1').find('a').each(function() {
    console.log($(this).attr('href'));
}
<a class="link" href="your_url" target="_top">Aan de Doorns Co-op </a>
我为这个做了一把小提琴,希望能有帮助


本打算要求提供html,但它刚刚发布。我建议把它放在一个查询中。你的代码在Waterbox上运行得很好,看起来很正常<代码>$('table.table1')。每个(函数(){console.log($(this.find('tr>td>strong>a')).attr('href');})
$(this.attr('href')
->
this.href
'.table1 strong a',以防万一。您使用的是什么
.get()
for?Map返回封装在jQuery包装器中的数组。要将其转换为普通数组,我们使用
get()。结果似乎完全一样,我知道,我知道。我希望有人提醒我这些参数,因为我以后经常使用它们;)。
$('.table1').find('a').each(function() {
    console.log($(this).attr('href'));
}
<a class="link" href="your_url" target="_top">Aan de Doorns Co-op </a>
console.log($(this).find('.link').attr('href'));