简体   繁体   English

获取带有图片的点击链接的 href

[英]get href of clicked link with image inside

Using jQuery, I'm simply trying to get the href of the a when the mouse clicks the image or the surrounding area.使用jQuery,我只是试图让href中的a ,当鼠标点击图像或周边地区。

HTML template: HTML模板:

<nav id="main_nav">
  <ul>
    <li>
      <a href="/" data-path>
        <img class="dashboard_icon" src="img/icons/nav/dashboard.svg">
      </a>
    </li>
  </ul>
  ... more nav options
</nav>

Styles:款式:

li { list-style: none; display: block; width: 80px; height: 80px; }
a { display: block; width: 100%; height: 100%; }
img { width: 30px; height: 30px; margin: 25px; }

jQuery: jQuery:

$(document).on('click', '[data-path]', (e) => {
  e.preventDefault();
  let target = $(e.target).attr('href');
  router.navigate(target);
});

But the problem is, whenever I click on the image inside the a , I don't get the href , as it tries to get the href value from the image that I clicked on and not the a .但问题是,每当我单击a内的图像时,我都不会得到href ,因为它试图从我单击的图像中获取href值,而不是a However, if I click on the area surrounding the image, I get the href as desired.但是,如果我单击图像周围的区域,则会根据需要获得href

Attempts:尝试:

I changed:我改变了:

let target = $(e.target).attr('href');

To:至:

let target = $(this).attr('href');

But get absolutely nothing when clicking the image or the surrounding area!但是单击图像或周围区域时什么也得不到!

What am I doing wrong?我做错了什么? I can't believe I'm asking questions about simple things like this but I've just taken a year off coding and I'm obviously missing something important, and it's baffling me.我不敢相信我在问这样简单的问题,但我刚刚从编码中抽出一年,显然我错过了一些重要的东西,这让我感到困惑。

I want to be able to click everything inside the a and get the href of the a .我希望能够点击里面的一切a和获得href中的a

您可以尝试e.currentTarget ,这将获取事件绑定到的元素, e.target 将获取实际单击的元素。

This might get you started:这可能会让你开始:

$(document).on('click', '#main_nav a', function(e) {
   e.preventDefault();
   let target = $(this).attr('href'); 
   alert(target)
});

See jsFiddle Demo参见jsFiddle 演示

Using jquery it should be:使用 jquery 应该是:

$(this).parent().attr('href');

Without using jquery this would be:如果不使用 jquery,这将是:

this.parentNode.getAttribute("href")

Complete code is:完整代码为:

$(document).on('click', 'image-class/id', (e) => {
  e.preventDefault();
 let target = $(this).parent().attr('href');
  router.navigate(target);
});

Image is child of anchor tag, you need to get his parent when you click on image so this done by this way.图像是锚标签的孩子,当你点击图像时,你需要得到他的父母,所以通过这种方式完成。

Demo: https://jsfiddle.net/ask3a4gf/演示: https : //jsfiddle.net/ask3a4gf/

尝试: var href = event.toElement.parentElement.href;

Your issue is coming from the use of an arrow function instead of function(e)您的问题来自使用箭头函数而不是function(e)

Change (e) => { to function(e) { and use $(this).attr('href')(e) => {更改为function(e) {并使用$(this).attr('href')

Quoting from mozilla -引用来自 mozilla -

An arrow function expression has a shorter syntax than a function expression and does not have its own this 箭头函数表达式的语法比函数表达式短,并且没有自己的this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM