Make a link open on double click

后端 未结 5 1653
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 06:12

How to make hyper link Link a double click link: i:e link should open on double click and single click should do nothing.

相关标签:
5条回答
  • 2020-12-03 06:45

    Great question, I have been looking for an answer. I came up with this (tested in Chrome). Add these attributes to your link:

    onclick="return false" ondblclick="location=this.href"
    

    e.g.

    <a onclick="return false" ondblclick="location=this.href" href="http://www.google.com">Google</a>
    
    0 讨论(0)
  • 2020-12-03 06:47

    You can do so using Javascript:

    <a href="#" onclick="javascripot:void(0)" ondblclick="javascript:DoMyWorkFunction()">a:x</a>
    
    0 讨论(0)
  • 2020-12-03 06:56

    Okay, so, you can do this:

    HTML:

    <a id='golink' href='gosomewhere.html'>Go Somewhere</a>
    

    JavaScript using jQuery:

    jQuery(function($) {
        $('#golink').click(function() {
            return false;
        }).dblclick(function() {
            window.location = this.href;
            return false;
        });
    });
    

    Live copy

    (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.)

    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.

    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;
            }
        });
    });​
    

    Live copy

    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 it without a really good use case.

    0 讨论(0)
  • 2020-12-03 06:57

    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>
    
    0 讨论(0)
  • 2020-12-03 07:08

    Try using span instead of a link. Something like this:

    <span ondblclick="window.location='http://www.google.com'" style="color:blue;text-decoration: underline;">Click Here</span>
    
    0 讨论(0)
提交回复
热议问题