我正在尝试检测是否使用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');
}
});
如何检测是否单击了特定链接?
发布于 2016-01-14 18:39:50
将事件处理程序添加到div中,单击并将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');
}
});https://stackoverflow.com/questions/34797259
复制相似问题