简体   繁体   English

检测使用href和onclick =“ setLocation()”单击的特定链接

[英]Detect specific link clicked with href and onclick=“setLocation()”

I am trying to detect if a link was clicked with JS . 我正在尝试检测JS是否单击了链接。 This is my code: 这是我的代码:

<div onclick="setLocation('http://domain.com/test')" class="gd-details" id="detail-box2">
    <h2 class="gd-product-name">
        <a title="test" href="http://domain.com/test">content 1</a>
    </h2>    
</div>

I'm also trying with this code and it doesn't works: 我也在尝试使用此代码,但它不起作用:

  $(document).on("click", "a", function() { var href = $(this).attr("href"); if(href == 'http://domain.com/test'){ alert('clicked'); } else { alert('not clicked'); } }); 

How do I detect if a specific link is clicked? 如何检测是否单击了特定链接?

Add an event handler to the div click and compare the onclick attribute to your test url. 将事件处理程序添加到div click并将onclick属性与您的测试U​​RL进行比较。

  $(document).on("click", "div", function() {
      var href = $(this).attr("onclick");
      if(href === "setLocation('http://domain.com/test')"){ 
        alert('clicked'); 
      } else {
        alert('not clicked'); 
      } 
    });

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

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