在IE9中不会触发onMove()事件

时间:2011-11-15 21:53:57

标签: javascript asp.net internet-explorer-9

来自demo link的代码适用于IE8,但不适用于IE9。这是什么原因?

如何强制它在IE9上运行?

代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Simple drag demo</title>
    <style>
#dragme {
  position:absolute;
  cursor:move;
  background:#eee;
  border:1px solid #333;
  padding:10px;
}
    </style>

<script>
function makeDraggable(element) {

  /* Simple drag implementation */
  element.onmousedown = function(event) {
    if(element.setCapture) { element.setCapture(); }

    document.onmousemove = function(event) {
      event = event || window.event;
      element.style.left = event.clientX + 'px';
      element.style.top = event.clientY + 'px';
    };

    document.onmouseup = function() {
      if(element.releaseCapture) { element.releaseCapture(); }

      document.onmousemove = null;
    };
  };

  /* These 3 lines are helpful for the browser to not accidentally 
   * think the user is trying to "text select" the draggable object.
   * Unfortunately they also break draggability outside the window.
   */
  element.unselectable = "on";
  element.onselectstart = function(){return false};
  element.style.userSelect = element.style.MozUserSelect = "none";
}
</script>

  </head>
  <body onload="makeDraggable(document.getElementById('dragme'))">
    <div id="dragme">Drag me (outside window)</div>
  </body>
</html>