检测链接上的按下并调用事件?
如果用户按下链接(而不是单击),我需要调用一个事件(例如 - 显示警报) - 并且我不希望它将他重定向到另一个页面。换句话说 - 当用户单击链接时 - 一切都像往常一样,但是当用户按下链接时,只会发生事件(显示警报)并且用户将留在我的页面中。这可能吗?
我所说的“按下”是指用户按下链接至少一秒钟。
谢谢。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
细化问题后,此代码将执行您要查找的操作:
注意:
return false
导致 href 不被执行,如果用户在链接上按住鼠标超过 1 秒(1000 毫秒),则会触发警报。
After you refined your question, this code does what you are looking for:
Notes:
return false
causes the href not to be executed,This will fire the alert if the user presses and holds the mouse over 1 second (1000 ms) on the link.
定义按。
您可能想要使用
mousedown
或keypress
事件,具体取决于您的意思。 (后一种情况可能需要检查事件以确保使用的是 Enter 键)Define press.
You probably want to use either the
mousedown
orkeypress
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)我的理解是你想要:
用户按下了链接 ->触发操作,但不重定向浏览器
用户单击链接(将鼠标悬停在您按下按钮的同一对象上)->重定向到不同的页面
以下代码可能会有所帮助
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
在 Jquery 中,您可以通过一种简单的方式做到这一点。
In Jquery you can do this in a simple way.