簡體   English   中英

檢測使用href和onclick =“ setLocation()”單擊的特定鏈接

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

我正在嘗試檢測JS是否單擊了鏈接。 這是我的代碼:

<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>

我也在嘗試使用此代碼,但它不起作用:

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

如何檢測是否單擊了特定鏈接?

將事件處理程序添加到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