简体   繁体   English

IE11中的dataTransfer无法正常工作

[英]dataTransfer not working correctly in IE11

I have noticed is that my code gives different output between browser versions. 我注意到,我的代码在浏览器版本之间给出了不同的输出。 Oddly using this with IE11 returns the path to the image and with chrome it returens the element ID. 奇怪的是,将其与IE11一起使用可返回图像的路径,而对于chrome,则可返回元素ID。 My question is, how do I need to alter my code for it to work? 我的问题是,我需要如何更改代码才能使其正常工作?

   <script>
    $(document).ready(function(){
            $("#test1, #test2, #test3").click(function testfunc(event) {
                var testvar = event.target.tagName;
                var testvarr = $(this).attr('id');
                var testvarrr = $(testvar + "#" + testvarr).attr("src");
                var target = $(event.target);
                alert(testvar);
                alert(testvarr);
                alert(testvarrr);// debug I will use this later for a overlay in the same window
            });
        })
        function allowDrop(ev) {
            ev.preventDefault();
        }
        function drag(ev) {
            ev.dataTransfer.setData("text", ev.target.id); // this is different between browsers 
        }

        function drop(ev) {
            ev.preventDefault();
            var data = ev.dataTransfer.getData("text");
            var array = ["#een", "#twee", "#drie"];

            for(i = 0; i < array.length; i++) {
                if($(array[i]).children().length < 1 && $(ev.target).attr('id') == array[i].replace("#", "")) {
                        if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
                            alert("hi you use IE");
                            alert(data);// returns the location of the image
                            document.getElementById(array[i].replace("#", "")).appendChild(document.getElementById(data));
                        }else{ 
                            document.getElementById(array[i].replace("#", "")).appendChild(document.getElementById(data));
                            alert("hello");
                            alert(data); // returns the ID of the element
                        }
                    return;
                }
            }
        }
    </script>
    <div class="midden">
        <div id="een" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
        <div id="twee" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
        <div id="drie" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    </div>
    <p id="demo"></p>
    <img id="test1" draggable="true" ondragstart="drag(event)" onclick="function testfunc(event)" src="ster.gif" width="118" height="69">
    <a href="ster.gif" target="_blank"><img id="test2" draggable="true" ondragstart="drag(event)" onclick="function testfunc(event)" src="ster.gif" width="118" height="69"></a><br>
    <a href="ster.gif" target="_blank"><img id="test3" draggable="true" ondragstart="drag(event)" onclick="function testfunc(event)" src="ster.gif" width="118" height="69"></a>

  $(document).ready(function() { $("#test1, #test2, #test3").click(function testfunc(event) { var testvar = event.target.tagName; var testvarr = $(this).attr('id'); var testvarrr = $(testvar + "#" + testvarr).attr("src"); var target = $(event.target); alert(testvar); alert(testvarr); alert(testvarrr); // debug I will use this later for a overlay in the same window }); }) function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.setData("text", ev.target.id); // this is different between browsers } function drop(ev) { ev.preventDefault(); var data = ev.dataTransfer.getData("text"); var array = ["#een", "#twee", "#drie"]; for (i = 0; i < array.length; i++) { if ($(array[i]).children().length < 1 && $(ev.target).attr('id') == array[i].replace("#", "")) { if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) { alert("hi you use IE"); alert(data); // returns the location of the image document.getElementById(array[i].replace("#", "")).appendChild(document.getElementById(data)); } else { document.getElementById(array[i].replace("#", "")).appendChild(document.getElementById(data)); alert("hello"); alert(data); // returns the ID of the element } return; } } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <div class="midden"> <div id="een" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div id="twee" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div id="drie" ondrop="drop(event)" ondragover="allowDrop(event)"></div> </div> <p id="demo"></p> <img id="test1" draggable="true" ondragstart="drag(event)" onclick="function testfunc(event)" src="ster.gif" width="118" height="69"> <a href="ster.gif" target="_blank"> <img id="test2" draggable="true" ondragstart="drag(event)" onclick="function testfunc(event)" src="ster.gif" width="118" height="69"> </a> <br> <a href="ster.gif" target="_blank"> <img id="test3" draggable="true" ondragstart="drag(event)" onclick="function testfunc(event)" src="ster.gif" width="118" height="69"> </a> 

Update: 更新:

Okay I fixed the code there were two things that I changed: 好吧,我修复了代码,我做了两件事更改:

-changed target to srcElement. -将目标更改为srcElement。
-removed the a tags(parents of images) -删除了a标签(图片的父母)

The a tag was removed because it changed the outcome of the data variable.(this wasn't a problem with chrome) So overall it seems that it might have been a problem with browser compatibility. 删除了一个标签,因为它更改了数据变量的结果。(这不是chrome的问题)因此,总体看来,这可能与浏览器兼容性有关。

Code that I changed from: ev.dataTransfer.setData("text", ev.srcElement.id); 我更改后的代码: ev.dataTransfer.setData("text", ev.srcElement.id); to: ev.dataTransfer.setData("text", ev.target.id); 到: ev.dataTransfer.setData("text", ev.target.id);

Okay I fixed the code there were two things that I changed: 好吧,我修复了代码,我做了两件事更改:

-changed target to srcElement. -将目标更改为srcElement。
-removed the a tags(parents of images) -删除了a标签(图片的父母)

The a tag was removed because it changed the outcome of the data variable.(this wasn't a problem with chrome) So overall it seems that it might have been a problem with browser compatibility. 删除了一个标签,因为它更改了数据变量的结果。(这不是chrome的问题)因此,总体看来,这可能与浏览器兼容性有关。

Code that I changed from: ev.dataTransfer.setData("text", ev.srcElement.id); 我更改后的代码: ev.dataTransfer.setData("text", ev.srcElement.id); to: ev.dataTransfer.setData("text", ev.target.id); 到: ev.dataTransfer.setData("text", ev.target.id);

I want to thank rfornal for helping me with this question. 我要感谢rfornal帮助我解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM