HTML 5& Javascript拖放IE浏览器

时间:2012-12-27 00:37:48

标签: javascript html5 internet-explorer drag-and-drop

我试图在IE 9和8(或至少IE 9)上仅使用Javascript和HTML 5进行拖放,但它不会工作,我读过拖放应该在IE上工作。你能看看我的代码并帮我解决这个问题吗?

代码:

<script type="text/javascript">
function allowDrop(ev){
    ev.preventDefault();
}

function drag(ev){
    var drag = ev.dataTransfer.setData("content", ev.target.id);

}

function drop(ev){
    ev.preventDefault();
    var image= ev.dataTransfer.getData("content");
    if ( image == ev.target.id) {
        ev.target.appendChild(document.getElementById(image));

    }

    else {

    }
}
</script>
<section id="planetBox">
<img class="planetImage" id="mercuryImg" draggable="true" 
    ondragstart="drag(event)" alt="A Planet" src="./images/planets/mercury.gif">  
</section>  

<div class="planet">
<section class="venusBox" id="venusImg" ondrop="drop(event)" 
    ondragover="allowDrop(event)"></section>
<section class="planetName"><h3>Venus</h3></section>    
</div>

1 个答案:

答案 0 :(得分:1)

尝试更改

var drag = ev.dataTransfer.setData("content", ev.target.id);
var image= ev.dataTransfer.getData("content");

var drag = ev.dataTransfer.setData("Text", ev.target.id);
var image= ev.dataTransfer.getData("Text");

你可能会考虑删除if条件

if ( image == ev.target.id) {