检测链接上的按下并调用事件?

发布于 2024-08-17 05:58:15 字数 176 浏览 3 评论 0原文

如果用户按下链接(而不是单击),我需要调用一个事件(例如 - 显示警报) - 并且我不希望它将他重定向到另一个页面。换句话说 - 当用户单击链接时 - 一切都像往常一样,但是当用户按下链接时,只会发生事件(显示警报)并且用户将留在我的页面中。这可能吗?

我所说的“按下”是指用户按下链接至少一秒钟。

谢谢。

I need to invoke an event (for example - display alert) if a user presses on a link (not clicks) - and I don't want it to redirect him to another page. In other words - when a user clicks a link - everything is as usual but when the user presses the link, just the event happens (the alert is displayed) and the user will stay in my page. Is this possible?

By saying press I mean that a user presses on a link for at least one second.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

厌倦 2024-08-24 05:58:15

细化问题后,此代码将执行您要查找的操作:

<script>
var timeout;
function onMouseDown(){
    timeout = window.setTimeout("alert(1)",1000);
}
function onMouseUp(){
    window.clearTimeout(timeout);
}
</script>

<a href="#" onmousedown="onMouseDown();return false;" onmouseup="onMouseUp();return false;">click</a>

注意:

  • return false 导致 href 不被执行,
  • 超时设置和清除可确保按下少于一秒的时间,不会触发该事件

如果用户在链接上按住鼠标超过 1 秒(1000 毫秒),则会触发警报。

After you refined your question, this code does what you are looking for:

<script>
var timeout;
function onMouseDown(){
    timeout = window.setTimeout("alert(1)",1000);
}
function onMouseUp(){
    window.clearTimeout(timeout);
}
</script>

<a href="#" onmousedown="onMouseDown();return false;" onmouseup="onMouseUp();return false;">click</a>

Notes:

  • The return false causes the href not to be executed,
  • The timeout set and clear makes sure that a press of less than a second, won't fire the event

This will fire the alert if the user presses and holds the mouse over 1 second (1000 ms) on the link.

停顿的约定 2024-08-24 05:58:15

定义

您可能想要使用 mousedownkeypress 事件,具体取决于您的意思。 (后一种情况可能需要检查事件以确保使用的是 Enter 键)

Define press.

You probably want to use either the mousedown or keypress event, depending on what you mean. (The latter case would probably involve checking the event to make sure it was the enter key that was used)

坚持沉默 2024-08-24 05:58:15

我的理解是你想要:

用户按下了链接 ->触发操作,但不重定向浏览器
用户单击链接(将鼠标悬停在您按下按钮的同一对象上)->重定向到不同的页面

以下代码可能会有所帮助

<html>
<head>

    <script type="text/javascript">
function mouse_pressed(){
    alert('pressed button');
}
function goToLink(url){
    window.location = url;
}
</script>
</head>
<body>

<label onclick="goToLink('http://www.google.com');" onmousedown="mouse_pressed();" >text</label>
<body>
</html>

What I understood is that you want:

user pressed the link -> fire an action, but don't redirect the browser
User clicks the link (mouse up over the same object where you pressed the button) -> redirect to a different page

The following code might help

<html>
<head>

    <script type="text/javascript">
function mouse_pressed(){
    alert('pressed button');
}
function goToLink(url){
    window.location = url;
}
</script>
</head>
<body>

<label onclick="goToLink('http://www.google.com');" onmousedown="mouse_pressed();" >text</label>
<body>
</html>
雨巷深深 2024-08-24 05:58:15

在 Jquery 中,您可以通过一种简单的方式做到这一点。

$("a").live('click',function(){
event.preventDefault();
});

In Jquery you can do this in a simple way.

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