检测 Href 被点击
我试图检测在卸载之前是否单击了某个元素。我无法让它工作。下面是项目中 Javascript 代码和 HTML 代码的示例(请注意,我无法控制 HTML 元素,因为它不是我的网站)
function checkLeave() {
var p = document.getElementByElementById('yeah');
if (p.href.onclick) {
//do something
}
else {
//do something else
}
}
window.onbeforeunload = checkLeave;
HTML CODE
//The goSomewhere goes to another page
<a id="yeah" href="javascript:goSomewhere();">
<img src="smiley.png">
</a>
提前致谢, J
I'm trying to detect if certain element is clicked on onbeforeunload. I can't get it to work. Below is examples of the Javascript code and HTML code on the project (Please note that I have no control over the HTML element as it is not my site)
function checkLeave() {
var p = document.getElementByElementById('yeah');
if (p.href.onclick) {
//do something
}
else {
//do something else
}
}
window.onbeforeunload = checkLeave;
HTML CODE
//The goSomewhere goes to another page
<a id="yeah" href="javascript:goSomewhere();">
<img src="smiley.png">
</a>
Thanks in advance,
J
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要做的是将事件处理程序绑定到页面上的每个
。
这可以通过以下方式完成:
What you need to do is bind an event handler to each
<a href=""></a>
on the page.This can be done with the following:
您正在测试
标记是否存在
onclick
属性。它不存在于标记中。该标记不使用onclick
,而是调用脚本作为元素的 href。因此,您需要在 href 中查找脚本:You are testing for the presence of the
onclick
property to the<a>
tag. It isn't present in the markup. Rather than using theonclick
, the markup calls a script as the element's href. So you need to look for a script in the href instead:也许是这样的? (只是想法)
Maybe something like this? (just the idea)