Javascript双击在IE9和IE11中不起作用

时间:2015-03-21 04:10:55

标签: javascript html css ajax double-click

在我的javascript代码中,单击打开新标签中的链接,然后双击打开灯箱。除IE9和IE11外,它适用于所有浏览器。 在我的第一个代码中,单击和双击工作,但单击,IE给出消息,"允许弹出?"我希望IE在新标签中打开链接,而不像其他浏览器那样打开消息。 在我的第二个代码中,单击可以按我的意愿工作,但是在IE中双击的第二次单击会被忽略,最终会单击一次。是否可以采取措施解决问题 - 无论是在第一个代码中还是在第二个代码中 - 我可能会错过这个问题?

第一个代码:

                       $('div[id^="jzl_"].short').click(function(e) {
                              var $this = $(this);
                              var currentID = e.target.id;

                              if ($this.hasClass('clicked')) {
                                     $this.removeClass('clicked');
                                     $.colorbox({
                                            href : "getInfo1.php?id=" + currentID,
                                            overlayClose : false,
                                            top : "16%"
                                     });
                                     //$.colorbox({ overlayClose: false });
                                     //alert("Double click");
                                     //here is your code for double click
                              } else {
                                     $this.addClass('clicked');
                                     setTimeout(function() {
                                            if ($this.hasClass('clicked')) {
                                                   $this.removeClass('clicked');
                                                   //                                 alert("Just one click!");
                                                   var jobSite = window.open('', '_blank');
                                                   sleep(1000);
                                                   var redirct = getPage(currentID);
                                                   sleep(1000);
                                                   jobSite.location = redirct;
                                                   //var redirct = getPage(currentID);
                                                   //window.open(redirct, '_newtab' + Math.floor(Math.random() * 999999));
                                            }
                                     }, 500);
                              }
                       });

第二代码:

                       $('div[id^="jzl_"].short').click(function(e) {
                              var $this = $(this);
                              var currentID = e.target.id;
                              var jobSite = window.open('', '_blank');
                              if ($this.hasClass('clicked')) {
                                     $this.removeClass('clicked');
                                     $.colorbox({
                                            href : "getInfo1.php?id=" + currentID,
                                            overlayClose : false,
                                            top : "16%"
                                     });
                                     //$.colorbox({ overlayClose: false });
                                     //alert("Double click");
                                     //here is your code for double click
                              } else {
                                     $this.addClass('clicked');
                                     setTimeout(function() {
                                            if ($this.hasClass('clicked')) {
                                                   $this.removeClass('clicked');
                                                   //                                 alert("Just one click!");
                                                   var redirct = getPage(currentID);
                                                   jobSite.location = redirct;
                                                   //var redirct = getPage(currentID);
                                                   //window.open(redirct, '_newtab' + Math.floor(Math.random() * 999999));
                                            }
                                     }, 500);
                              }
                       });

2 个答案:

答案 0 :(得分:0)

您应该同时使用.click()和.dblclick(),而不是测试该元素是否具有clicked类。所以这是新代码:

$('div[id^="jzl_"].short').dblclick(function(e) {

var $this = $(this);
var currentID = e.target.id;

$.colorbox({
href : "getInfo1.php?id=" + currentID,
overlayClose : false,
top : "16%"
});

//$.colorbox({ overlayClose: false });
//alert("Double click");
//here is your code for double click

});



$('div[id^="jzl_"].short').click(function(e) {

 var $this = $(this);
 var currentID = e.target.id;

 var jobSite = window.open('', '_blank');
 var redirct = getPage(currentID);
jobSite.location = redirct;

//var redirct = getPage(currentID);
//window.open(redirct, '_newtab' + Math.floor(Math.random() * 999999));

});

答案 1 :(得分:0)

此解决方案应该有效:

var t = []; //timeout array
$('div[id^="jzl_"].short')
    .on('click', function(e) {
        var $this = $(this),
            currentID = e.target.id;

        // put each click in timeout array
        t[t.length] = setTimeout(function() {
            t = []; //reset timeout array
            alert('clicked: ' + currentID);

            //your code here

        }, 500);
    })
    .on('dblclick', function (e) {

        //cancel 2 timeouts and reset timeout array
        clearTimeout(t[0]);
        clearTimeout(t[1]);
        t = [];

        var $this = $(this),
            currentID = e.target.id;

        window.open('http://google.com', '_blank');
    });

http://jsfiddle.net/tvL07phr/2/

演示