Jquery create Double Click event on A Href

纵然是瞬间 提交于 2019-12-23 12:24:23

问题


Guys is it possible to create double click event for the a href using jquery


回答1:


The problem with performing an action on double click of an anchor is that the page will redirect on the first click, preventing the double click from responding in time.

If you want to "intercept" the click event so that the double click event has a chance to fire before the page redirects, then you may have to set a timeout on the click like this:

$('a').click(function () {
    var href = $(this).attr('href');

    // Redirect only after 500 milliseconds
    if (!$(this).data('timer')) {
       $(this).data('timer', setTimeout(function () {
          window.location = href;
       }, 500));
    }
    return false; // Prevent default action (redirecting)
});

$('a').dblclick(function () {
    clearTimeout($(this).data('timer'));
    $(this).data('timer', null);

    // Do something else on double click

    return false;
});

Demo: http://jsfiddle.net/4788T/1/




回答2:


if you a link has id of "id", then:

$("#id").bind("dblclick", ....);


来源:https://stackoverflow.com/questions/4818085/jquery-create-double-click-event-on-a-href

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!