6个回答

4
如果你想引用元素的特定属性,可以使用jQuery的attr函数:
$(document.body).on('click',"a",function(event){ 
    console.log( $(this).attr("href") );
});

1
$('body').on('click', 'a', function () { 
    console.log($(this).attr('href'));
});

1
$(document.body).on('click',"a",function(event){ 
    console.log($(this).attr('href'));
    event.preventDefault();
}); 

1

$( "a" )[0].href 或者 $( "a" ).attr( "href" )


0

你可以使用这段代码获取完整的 href 或者只获取 hash:

$("body").on("click","a",function() {
  var href = $(this).attr("href")
  var hash = href.replace(/^.*?#/,'');
  console.log(href + " - " + hash);
});

你可以在JSFiddle上看到一个演示


0
根据您的代码,单击“a”标签将在数组中为$(this)提供页面中所有锚点对象的列表。每个对象都有特定于jQuery的属性(如用jQuery包装的this)例如href、hash等。
因此,如果您只有一个锚点,则可以使用$(this)[0].href 但更具体的解决方案是使用$(this).attr("href")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接