简体   繁体   English

HTML5拖放无法正常工作

[英]HTML5 drag and drop not working

I am using an HTML5 drag and drop script and it doesn't work. 我正在使用HTML5拖放脚本,因此无法正常工作。 When I use the error console in safari, it says : 当我在野生动物园中使用错误控制台时,它说:

NOT_FOUND_ERR: DOM Exception 8: An attempt was made to reference a
Node in a context where it does not exist.

I have no idea what that means. 我不知道那是什么意思。 Here is my example: http://thomaswd.com/maze . 这是我的示例: http : //thomaswd.com/maze Try dragging the mouse to the indicated square. 尝试将鼠标拖动到指示的正方形。 It's supposed to make Algernon stay at that box. 应该让阿尔及农留在那个盒子里。 Thanks! 谢谢!

Also, is it possible to stop the user from dragging the mouse through the walls? 另外,是否可以阻止用户将鼠标拖动穿过墙壁? Thanks!! 谢谢!!

Collision for the walls 撞墙
http://www.html5rocks.com/en/tutorials/canvas/notearsgame/ http://www.html5rocks.com/zh-CN/tutorials/canvas/notearsgame/

as far as the mouse and error goes, I don't see either. 就鼠标和错误而言,我也没有看到。 However the error listed above is obvs. 但是,上面列出的错误是obvs。 a issue with your source/script. 您的源代码/脚本有问题。 I looked through it, but there is nothing there, did you remove the JS before posting the example?\\ 我浏览了一下,但是那里什么也没有,您在发布示例之前删除了JS吗?\\

Edit 编辑

ev.target.appendChild(document.getElementById(data)); ev.target.appendChild(的document.getElementById(数据)); this line. 这条线。 "Error: An attempt was made to reference a Node in a context where it does not exist. at drop (thomaswd.com/Maze/:18:15) at HTMLDivElement.ondrop (thomaswd.com/Maze/:50:225)" “错误:尝试在不存在的上下文中引用节点。在HTMLDivElement.ondrop(thomaswd.com/Maze/:50:225)放下(thomaswd.com/Maze/:18:15)时”

The doms (getelementbyid) is looking for the ID "mouse" and can't find it. dom(getelementbyid)正在寻找ID“鼠标”,但找不到它。 set the image ID = "mouse" 设置图像ID =“鼠标”

I used chromes developer tools and edit this line locally and it worked. 我使用了chromes开发人员工具,并在本地编辑了此行,就可以了。 and the mouse dropped without an issue. 鼠标掉落没有问题。

<img src="rat.png" id="mouse" width="60px" ondragstart="drag(event)" style="cursor:move;" draggable="true">

This happens because you did not set id of your mouse. 发生这种情况是因为您没有设置鼠标ID。 add this to your mouse image 将此添加到您的鼠标图像

id="mouse"

Second, for every single TD, you need to make it droppable by setting these attributes. 其次,对于每个单个TD,需要通过设置这些属性使其可放置。

ondrop="drop(event)" ondragover="allowDrop(event)"

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

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