检测 Href 被点击

发布于 2024-12-08 02:30:07 字数 502 浏览 1 评论 0原文

我试图检测在卸载之前是否单击了某个元素。我无法让它工作。下面是项目中 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋心╮凉 2024-12-15 02:30:07

您需要做的是将事件处理程序绑定到页面上的每个
这可以通过以下方式完成:

// Select all links
//var allLinks = document.querySelectorAll('a[href]');
var allLinks = document.links;

// Bind the event handler to each link individually
for (var i = 0, n = allLinks.length; i < n; i++) {
    //allLinks[i].addEventListener('click', function (event) {});
    allLinks[i].onclick = function () {
        // Do something            
    };
}

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:

// Select all links
//var allLinks = document.querySelectorAll('a[href]');
var allLinks = document.links;

// Bind the event handler to each link individually
for (var i = 0, n = allLinks.length; i < n; i++) {
    //allLinks[i].addEventListener('click', function (event) {});
    allLinks[i].onclick = function () {
        // Do something            
    };
}
沉默的熊 2024-12-15 02:30:07

您正在测试 标记是否存在 onclick 属性。它不存在于标记中。该标记不使用 onclick,而是调用脚本作为元素的 href。因此,您需要在 href 中查找脚本:

var p = document.getElementByElementById('yeah');
if (p.href.indexOf("javascript") === 0) {
   //do something
}
else {
   // do something else
}

You are testing for the presence of the onclick property to the <a> tag. It isn't present in the markup. Rather than using the onclick, the markup calls a script as the element's href. So you need to look for a script in the href instead:

var p = document.getElementByElementById('yeah');
if (p.href.indexOf("javascript") === 0) {
   //do something
}
else {
   // do something else
}
时间你老了 2024-12-15 02:30:07

也许是这样的? (只是想法)

document.getElementById('yeah').onclick = function() {
  clicked = this.href;
};

Maybe something like this? (just the idea)

document.getElementById('yeah').onclick = function() {
  clicked = this.href;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文