JS不适用于Firefox和Explorer

时间:2016-03-01 10:06:54

标签: javascript html css

我的代码有问题。 为什么在Firefox和Internet Explore上不起作用? 此代码在Google Chrome上运行良好。

JS:

    var i=1;

function dragStart(ev) {
  ev.dataTransfer.effectAllowed='move';
  ev.dataTransfer.setData("Text", ev.target.getAttribute('id'));
  ev.dataTransfer.setDragImage(ev.target,50,50);
  return true;
}

// these functions prevents default behavior of browser
function dragEnter(ev) {
  event.preventDefault();
  return true;
}
function dragOver(ev) {
  event.preventDefault();
}

// function defined for when drop element on target
function dragDrop(ev) {

  var data = ev.dataTransfer.getData("Text");
  ev.target.appendChild(document.getElementById(data));
   if (ev.shiftKey)
  {

    var nodeCopy = document.getElementById(data).cloneNode(true);
    nodeCopy.id = "newId"+i; /* We cannot use the same ID */
    ev.target.appendChild(nodeCopy);
    i++;

  }
  else
    ev.target.appendChild(document.getElementById(data));

  ev.stopPropagation();
  return false;
}

HTML:

    <article id="dropIt" ondragenter="return dragEnter(event)" ondrop="return dragDrop(event)" ondragover="return dragOver(event)"></article>

<article id="holder" class="no-print" ondragenter="return dragEnter(event)" ondrop="return dragDrop(event)" ondragover="return dragOver(event)">
<div class="drag" id="boxB" draggable="true" ondragstart="return dragStart(event)"></div>
</article>

为什么在Firefox和Internet Explore上不起作用? 感谢

1 个答案:

答案 0 :(得分:0)

您必须像这样正确地声明事件

// these functions prevents default behavior of browser
function dragEnter(event) {
  event.preventDefault();
  return true;
}
function dragOver(event) {
  event.preventDefault();
}