我正在尝试将图像拖放到div上。图像不会被拖到div上并给出以下错误
Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.draganddrop.html:20 dropdraganddrop.html:26 ondrop
代码
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Creativity Dashboard</title>
<!-- Required CSS -->
<link href="css/movingboxes.css" rel="stylesheet">
<link href="css/compare.css" rel="stylesheet">
<!--[if lt IE 9]>
<link href="css/movingboxes-ie.css" rel="stylesheet" media="screen">
<![endif]-->
<!-- Required script -->
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script src="js/jquery.movingboxes.js"></script>
<!-- Demo only -->
<link href="demo/demo.css" rel="stylesheet">
<script src="demo/demo.js"></script>
<script>
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}
</script>
<style>
/* Overall & panel width defined using css in MovingBoxes version 2.2.2+ */
#slider-one {
width: 1003px;
}
#slider-one>li {
width: 150px;
}
</style>
</head>
<body>
<div class="wrapper">
<!-- Slider #1 -->
<ul id="slider-one">
<li><img id="drag1" src="demo/1.jpg" draggable="true"
ondragstart="drag(event)" alt="picture"></li>
<li><img id="drag2" src="demo/2.jpg" draggable="true"
ondragstart="drag(event)" alt="picture"></li>
<li><img id="drag3" src="demo/3.jpg" draggable="true"
ondragstart="drag(event)" alt="picture"></li>
<li><img id="drag5" src="demo/4.jpg" draggable="true"
ondragstart="drag(event)" alt="picture"></li>
<li><img id="drag6" src="demo/5.jpg" draggable="true"
ondragstart="drag(event)" alt="picture"></li>
<li><img id="drag7" src="demo/6.jpg" draggable="true"
ondragstart="drag(event)" alt="picture" id="astronaut"></li>
<li><img id="drag8" src="demo/7.jpg" draggable="true"
ondragstart="drag(event)" alt="picture"></li>
</ul>
<!-- end Slider #1 -->
<div id="dragAnddrop" ondrop="drop(event)"
ondragover="allowDrop(event)" style="width: 12em; height: 12em">
</div>
</div>
<div>
<div class="left">
<img id="drag" draggable="true" ondragstart="drag(event)"
src="images/startingImage.jpg" style="width: 12em;" alt="picture">
</div>
<div class="middle ">
<img id="image3" src="images/startingImage.jpg" class="image-size"
alt="picture" draggable="true" ondragstart="drag(event)"> <img
src="images/harvest.jpg" class="image-size" alt="picture">
</div>
<div class="right">
<img src="images/startingImage.jpg" style="width: 12em;"
alt="picture">
</div>
</div>
</body>
</html>
答案 0 :(得分:132)
这实际上是一个简单的答案。
您的函数返回的是字符串而不是div
节点。 appendChild
只能附加一个节点。
例如,如果您尝试appendChild字符串:
var z = '<p>test satu dua tiga</p>'; // is a string
document.body.appendChild(z);
以上代码永远不会有效。将起作用的是:
var z = document.createElement('p'); // is a node
z.innerHTML = 'test satu dua tiga';
document.body.appendChild(z);
答案 1 :(得分:9)
如果您不小心拖动了已分配id的元素(例如,您正在拖动周围的元素),则会发生这种情况。在这种情况下,ID为空,函数drag()分配一个空值,然后传递给drop()并在那里失败。
尝试将id分配给所有元素,包括tds,div或可拖动元素周围的任何元素。
答案 2 :(得分:2)
在我的情况下,我没有调用appendChild
的字符串,我传递divel.appendChild(childel[0])
参数的对象是错误的,它是一个数组,我传递了一个元素对象,所以我使用了divel.appendChild(childel)
而不是export const ROUTES: RouteInfo[] = [
{ path: '/dashboard', title: 'Dashboard', icon: 'fa fa-tachometer', class: '' ,name:'',src:"../../../../assets/images/header-icon.png",srcOn:"../../../../assets/images/dashboard_highlighted.png"},
{ path: '/forms', title: 'Forms', icon:'fa fa-eyedropper', class: '',name:'',src:"../../../../assets/images/dashboard_highlighted.png",srcOn:""},
{ path: '/tables', title: 'Desk Blotter', icon:'fa fa-pencil', class: '' ,name:'',src:"../../../../assets/images/dashboard_highlighted.png",srcOn:""},
{ path: '/bs-element', title: 'Admin', icon:'fa fa-picture-o', class: '' ,name:'',src:"../../../../assets/images/dashboard_highlighted.png",srcOn:""},
{ path: '/login', title: 'Logout', icon:'fa fa-power-off', class: '',name:'',src:"../../../../assets/images/dashboard_highlighted.png",srcOn:"" },
];
<li *ngFor="let menuItem of menuItems" routerLinkActive="active" class="{{menuItem.class}}" >
<a [routerLink]="[menuItem.path]" name="{{menuItem.name}}">
<img src="{{menuItem.src}}" style="width:66%;" (mouseenter)="'src=''{{menuItem.srcOn}}'" id="{{menuItem.title}}"/> {{menuItem.title}}
</a>
</li>
而且效果很好。希望它对某人有所帮助。
答案 3 :(得分:1)
您也可以使用element.insertAdjacentHTML('beforeend', data);
请阅读MDN上的“安全注意事项”。
答案 4 :(得分:0)
使用ondragstart(event)
代替ondrag(event)