简体   繁体   English

对方法或属性访问的意外调用?

[英]unexpected call to method or property access?

I have an error on a drag and drop example that I have been trying to learn from and I cannot figure out why this error keeps coming up.我在拖放示例中遇到了一个错误,我一直试图从中学习,但我无法弄清楚为什么这个错误不断出现。

Here is my HTML:这是我的 HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Week 3 Drag and Drop</title>
<link rel="stylesheet" type="text/css" href="Week3DragAndDropCSS.css">
<script src="Week3DragAndDropJS.js"></script>
</head>
<body>
<header>
    <h1>Week 3 Drag and Drop</h1>
</header>
    <div id="trashCan" ondragover="dragElementOver(event)" 
 ondrop="drop(event)"></div>
    <div id="trash1" draggable="true" ondragstart="startDrag
 (event)">Im Trash</div>


<footer>

</footer>

  </body>

   </html>

Here is my CSS:这是我的 CSS:

header{

 }
body{

 }
footer{

}
#trashCan {
height:200px;
width:200px;
background-image: url(trash.png);
background-repeat:no-repeat;
background-size:contain;
 }
 #trash1{
height:200px;
width:200px;
background: red;
text-align: center;
 }

Then the JavaScript:然后是 JavaScript:

function startDrag(e){
console.log("drag event started.");
e.dataTransfer.setData('Color', e.target.getAttribute('id'));
}
function dragElementOver(e){
e.preventDefault();
console.log("You entered into the drop zone.");
}
function drop(e){
var data = e.dataTransfer.getData("Color")
console.log("You have dropped the trash into the trash can " + data);
}

The part were the error occurs is in the javascript at the e.dataTransfer.setData part I have rewritten it several ways and looked up on google and still cant figure it out.发生错误的部分是在 e.dataTransfer.setData 部分的 javascript 中,我已经用几种方式重写了它,并在 google 上查找,但仍然无法弄清楚。 I thought the console.log methods would help me identify whats going on but they really didn't help.我认为 console.log 方法可以帮助我确定发生了什么,但它们确实没有帮助。

Apparently a IE bug HTML5 Drag&Drop issue in Internet Explorer (dataTransfer property access not possible)显然是Internet Explorer 中的 IE 错误HTML5 拖放问题(无法访问 dataTransfer 属性)

You should replace "Color" by "text" in your code您应该在代码中用“文本”替换“颜色”

e.dataTransfer.setData('text', e.target.getAttribute('id'));

And Then :进而 :

var data = e.dataTransfer.getData("text")

My bad !我的错 ! Did not read well the documentation !没有很好地阅读文档!

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

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