Javascript 双击打开链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4562012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make a link open on double click
提问by Muhammad Zeeshan
How to make hyper link <a>Link</a>
a double click link: i:e link should open on double click and single click should do nothing.
如何使超链接<a>Link</a>
成为双击链接:i:e 链接应该在双击时打开,单击什么都不做。
回答by T.J. Crowder
Okay, so, you cando this:
好的,所以,你可以这样做:
HTML:
HTML:
<a id='golink' href='gosomewhere.html'>Go Somewhere</a>
JavaScript using jQuery:
使用 jQuery 的 JavaScript:
jQuery(function($) {
$('#golink').click(function() {
return false;
}).dblclick(function() {
window.location = this.href;
return false;
});
});
(It doesn't have to be an ID; you can do this with a class or anything else that lets you form a selector that jQuery can process to hook things up.)
(它不必是一个 ID;你可以用一个类或其他任何东西来做这个,让你形成一个选择器,jQuery 可以处理它来连接东西。)
If the user has JavaScript disabled, the link will work normally. Crawlers will find the link normally, etc. If a user has JavaScript enabled, the event handlers will get hooked up and it will require a double click.
如果用户禁用了 JavaScript,链接将正常工作。爬虫会正常找到链接,等等。如果用户启用了 JavaScript,事件处理程序将被连接起来,需要双击。
The above blows away keyboard navigation, though, so then you have to handle that:
但是,上面的内容会破坏键盘导航,因此您必须处理:
jQuery(function($) {
$('#golink').click(function() {
return false;
}).dblclick(function() {
window.location = this.href;
return false;
}).keydown(function(event) {
switch (event.which) {
case 13: // Enter
case 32: // Space
window.location = this.href;
return false;
}
});
});?
I can't imagine this is good for accessibility, and I bet there are other things not catered for above. Which all feeds into:
我无法想象这对可访问性有好处,而且我敢打赌还有其他事情没有满足以上要求。其中所有内容:
But I'd strongly recommend against doing itwithout a really gooduse case.
但是我强烈建议不要在没有非常好的用例的情况下这样做。
回答by goenning
Try using span instead of a link. Something like this:
尝试使用 span 而不是链接。像这样的东西:
<span ondblclick="window.location='http://www.google.com'" style="color:blue;text-decoration: underline;">Click Here</span>
回答by Chalky
Great question, I have been looking for an answer. I came up with this (tested in Chrome). Add these attributes to your link:
好问题,我一直在寻找答案。我想出了这个(在 Chrome 中测试)。将这些属性添加到您的链接:
onclick="return false" ondblclick="location=this.href"
e.g.
例如
<a onclick="return false" ondblclick="location=this.href" href="http://www.google.com">Google</a>
回答by Bhaskar
You can do so using Javascript:
您可以使用 Javascript 执行此操作:
<a href="#" onclick="javascripot:void(0)" ondblclick="javascript:DoMyWorkFunction()">a:x</a>
回答by bharath
try this:
尝试这个:
<a ondblclick= "openLink('your_link')">Link</a>
<script>
function openLink(link)
{
location.href = link;
}
</script>
OR
或者
<a ondblclick="location.href='your_link'">Double click on me</a>