我正在尝试检测是否使用 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
属性与您的测试 URL 进行比较。
$(document).on("click", "div", function() {
var href = $(this).attr("onclick");
if(href === "setLocation('http://domain.com/test')"){
alert('clicked');
} else {
alert('not clicked');
}
});
关于javascript - 检测使用 href 和 onclick ="setLocation()"单击的特定链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34797259/