我想捕获链接点击并存储在我的数据库中,而无需使用旋转门。当前代码

$( ".ads-box a" ).click(function( event ) {
  event.preventDefault();
  var href = $(this).find('a').attr('href');
  var id = $(this).find('a').attr('id')
  console.log(href);
  console.log(id);
  console.log(this);
});




<div class="ads-box" id="adbox-35"><a href="http://www.makeitrightnola.org" id="ad-35" target="_blank"><img src="/g/shows/sidebar/83172823_ad_image.png" alt="Make It Right"></a></div>


在控制台中产生

undefined
undefined
<a href="http://www.makeitrightnola.org" id="ad-35" target="_blank"><img src="/g/shows/sidebar/83172823_ad_image.png" alt="Make It Right"></a>


如何访问链接的属性?怎么了?

最佳答案

删除.find('a')中的var href = $(this).find('a').attr('href');。您已经在<a>元素上单击“ ing”,因此对于$(this).attr('href');

尝试这个:

$( ".ads-box a" ).click(function( event ) {
  event.preventDefault();
  var href = this.href;  // i used vanilla JS here
  var id = this.id;      // i used vanilla JS here
  console.log(href);
  console.log(id);
  console.log(this);
});


演示here

10-07 19:24
查看更多