如何检测Internet Explorer 10 +

时间:2015-08-15 02:20:40

标签: javascript internet-explorer events detail

我需要知道如何检测HTML元素的点击次数。使用Firefox和Chrome,我使用“event”对象并检查其“detail”属性。使用“mousedown”处理程序,我只想在第一次单击时启动对元素的“拖动”(使用CSS在屏幕上移动):

if(event.detail> 1)返回;

但Internet Exploder 11(我假设10+相同) event.detail === 5 在第一次点击。 IE9返回“正确”值1。

我唯一能想到的是定期(每隔0.5秒左右)使用“setInterval()”将“全局”值设置为= 0,然后在每个“mousedown”上增加该值并使用该值而不是“event.detail”。

荒谬可笑。

您可以在以下位置查看此问题(直至我修复)

http://softmoon-webware.com/MasterColorPicker_instructions.php

有问题的实际javascript代码位于文件中(最后):

http://softmoon-webware.com/color-pickers/SoftMoon-WebWare/MasterColorPicker2.js

点击左侧的输入框(表示尝试此处)后,您应该可以通过屏幕周围的手柄“拖动”“选取器面板”。使用真正的浏览器没有问题,即使IE9与代码库有关(是的,它会引发堆栈溢出错误!)它会(或者在上次更新之前使用“FD-sliders”实现了启动堆栈溢出问题)允许拖动面板。 IE10 +仅“突出显示”(选择)光标下的文本,不会再拖动,因为“错误”的“event.detail”值。公平地说,我在微软开发者网络页面上找不到的东西说明了event.detail规范究竟是什么(只是“提供有关事件的更多信息......”),而且W3C页面的链接似乎没有“event.detail”属性的规范。

我在这里错过了什么吗?

从文件中提取代码:

for (var i=0, handle, panels=MasterColorPicker.panels;  i<panels.length;  i++)  {
    if (panels[i]===MasterColorPicker.mainPanel)  continue;
    if (panels[i].id==='MasterColorPicker_options')  {
        handle=panels[i].getElementsByTagName('header')[0];             // ↓ ↓ for drag, the first panel must be the largest and contain the other(s) in its margin
        UniDOM.addEventHandler(handle, 'onmousedown', dragPanel, false, [MasterColorPicker.mainPanel, panels[i]]);
        UniDOM.addEventHandler(handle, 'onmouseup', returnPanelsOn3, false, [MasterColorPicker.mainPanel, panels[i]]);  }
    else  {
        handle=panels[i].getElementsByTagName('h2')[0].getElementsByTagName('span')[0];
        UniDOM.addEventHandler(handle, 'onmousedown', dragPanel, false, [panels[i]]);
        UniDOM.addEventHandler(handle, 'onmouseup', returnPanelsOn3, false, [panels[i]]);  }
    UniDOM.addEventHandler(handle, 'oncontextmenu', abortContextMenu);  }
UniDOM.addEventHandler(document.getElementById("MasterColorPicker_returnPanelsOn3"), 'onmouseup', returnPanelsOn3, false, panels);
function dragPanel(event, stickyPanels)  { console.log("IE sucks: detail: "+event.detail);
    event.stopPropagation();
    event.preventDefault();
    if (event.detail>1  ||  !MasterColorPicker.enablePanelDrag)  return;
    var stick=(event.shiftKey || event.button===2) && MasterColorPicker.enableStickyPanels && (UniDOM.MS_exploder!==9),
            ttcn= (stick ? 'MCP_thumbtack' : ""),
            CSS=getComputedStyle(stickyPanels[0], null),
            mOff= (CSS.position==='fixed') ?
                    {x: (document.body.offsetWidth-event.clientX)-parseInt(CSS.right),  y: event.clientY-parseInt(CSS.top)}
                : UniDOM.getMouseOffset(stickyPanels[0], event),
        dragHandle=event.currentTarget,
            move=UniDOM.addEventHandler(document.body, 'onmousemove', function(event)  {
                var CSS=getComputedStyle(stickyPanels[0], null);
                if (CSS.position==='fixed')
                var b={w: document.body.offsetWidth, h: document.documentElement.clientHeight || window.innerHeight, x: 0, y: 0},
                        y=(event.clientY - mOff.y),
                        x=((b.w-event.clientX) - mOff.x);
                else
                var b=UniDOM.getElementOffset(stickyPanels[0].offsetParent, MasterColorPicker.dragBounder),
                    b={y: b.y, x: b.x, w: MasterColorPicker.dragBounder.offsetWidth, h: MasterColorPicker.dragBounder.offsetHeight},
                    m=UniDOM.getMouseOffset(stickyPanels[0].offsetParent, event),
                        y=m.y - (parseInt(CSS.marginTop) + mOff.y),
                        x=(b.w-m.x) - (stickyPanels[0].offsetWidth-mOff.x) + parseInt(CSS.marginRight);
                y= (y<-b.y) ?  (-b.y)  :  ( (y>(m=b.h-(stickyPanels[0].offsetHeight+parseInt(CSS.marginTop)+parseInt(CSS.marginBottom)+b.y))) ? m : y );
                x= (x<-b.x) ?  (-b.x)  :  ( (x>(m=b.w-(stickyPanels[0].offsetWidth+parseInt(CSS.marginLeft)+parseInt(CSS.marginRight)+b.x))) ? m : x );
                for (i=0;  i<stickyPanels.length;  i++)  {
                    stickyPanels[i].style.top= y + 'px';
                    stickyPanels[i].style.right= x + 'px';  }
                event.stopPropagation();
                event.preventDefault();  }
            , true),
            blockMenu=UniDOM.addEventHandler(document.body, 'oncontextmenu', abortContextMenu, true),
            drop=UniDOM.addEventHandler(document.body, 'onmouseup', function(event)  {
                move.onmousemove.remove();  blockMenu.oncontextmenu.remove();  drop.onmouseup.remove();
                event.stopPropagation();
                event.preventDefault();
              for (var i=0;  i<stickyPanels.length;  i++)  {UniDOM.removeClass(stickyPanels[i], ['dragging', ttcn]);}
                UniDOM.removeClass(document.body, ['MCP_drag', ttcn]);
                if (stick) dragHandle.removeChild(MasterColorPicker.thumbtackImage);
                try {MasterColorPicker.dataTarget.focus();} catch(e) {}  }
            , true);
  for (var i=0;  i<stickyPanels.length;  i++)  {
      UniDOM.addClass(stickyPanels[i], ['dragging', ttcn]);
        MasterColorPicker.setTopPanel(stickyPanels[i]);  }
    if (stick)  {
        mOff.x=stickyPanels[0].offsetWidth-mOff.x;
        if (CSS.position==='fixed')  {
            mOff.y= -(parseInt(CSS.marginTop)-mOff.y);
            var currentCN='floating', newCN='scrollable';  }
        else  {
            mOff.y += parseInt(CSS.marginTop);
            var currentCN='scrollable', newCN='floating';  }
      while (--i>=0)  {UniDOM.swapOutClass(stickyPanels[i], currentCN, newCN);}
      dragHandle.appendChild(MasterColorPicker.thumbtackImage);
      move.onmousemove.wrapper(event);  }
    UniDOM.addClass(document.body, ['MCP_drag', ttcn]);  }
function returnPanelsOn3(event, stickyPanels)  {
    event.stopPropagation();
    event.preventDefault();
    if (event.detail!==3  ||  event.button!==0)  return;
    MasterColorPicker.returnPanelsHome(stickyPanels);  }
function abortContextMenu(event) {event.preventDefault();  event.stopPropagation();}
MasterColorPicker.returnPanelsHome=function(stickyPanels)  {
  for (var i=0;  i<stickyPanels.length;  i++)  {
        stickyPanels[i].style.top= "";
        stickyPanels[i].style.right= "";
        UniDOM.removeClass(stickyPanels[i], ['scrollable', 'floating']);  }  }

1 个答案:

答案 0 :(得分:0)

只需计算你的mousedown处理程序被调用的次数:

var clickCount = 0;
$('#myElement').on('mousedown', function(){
    clickCount++;
    if (clickCount > 1)
        return;
    [ ... ]
});