Javascript 为什么我';m GettingUncought TypeError:未能执行';追加儿童';在';节点';:参数1的类型不是';节点';。使用这个可拖动的代码?
我使用的代码来自Web Dev Simplified: JS: 我花了太多时间在C#作为后端,我发现自己在与一些前端JS纠缠不清,然后只是复制了上面的代码 但我不能让它在我的代码上工作。我的代码有另一个代码,用于预览要上载的图像:Javascript 为什么我';m GettingUncought TypeError:未能执行';追加儿童';在';节点';:参数1的类型不是';节点';。使用这个可拖动的代码?,javascript,asp.net,Javascript,Asp.net,我使用的代码来自Web Dev Simplified: JS: 我花了太多时间在C#作为后端,我发现自己在与一些前端JS纠缠不清,然后只是复制了上面的代码 但我不能让它在我的代码上工作。我的代码有另一个代码,用于预览要上载的图像: function previewImages() { let aux = document.getElementById("preview-multiple"); if (aux.hasChildNodes()) {
function previewImages() {
let aux = document.getElementById("preview-multiple");
if (aux.hasChildNodes()) {
while (aux.firstChild) {
aux.removeChild(aux.firstChild);
}
}
if (this.files) {
[].forEach.call(this.files, readAndPreview);
}
function readAndPreview(file) {
if (!/\.(jpe?g|png|gif)$/i.test(file.name)) {
return alert(file.name + " is not an image");
}
var div = document.createElement('div');
var reader = new FileReader();
reader.addEventListener("load", function () {
var image = new Image();
image.height = 100;
image.title = file.name;
image.src = this.result;
image.className = "draggable";
image.draggable = true;
//draggable.js dependent of previewImage.js
aux.appendChild(div);
div.appendChild(image)
});
reader.readAsDataURL(file);
}
}
在这段代码中,我写道:
image.className = "draggable";
image.draggable = true;
让它可以拖动。并将其包装成div,以便更好地组织
但我在尝试拖动时遇到以下错误:
我真的不知道我在哪里失败了
下一步将使用视图中的顺序将图像存储在数据库中,但这是另一个未来的问题
你能帮我吗?谢谢。我找到问题了。问题是在将图像插入DOM之前脚本正在运行
function previewImages() {
let aux = document.getElementById("preview-multiple");
if (aux.hasChildNodes()) {
while (aux.firstChild) {
aux.removeChild(aux.firstChild);
}
}
if (this.files) {
[].forEach.call(this.files, readAndPreview);
}
function readAndPreview(file) {
if (!/\.(jpe?g|png|gif)$/i.test(file.name)) {
return alert(file.name + " is not an image");
}
var div = document.createElement('div');
var reader = new FileReader();
reader.addEventListener("load", function () {
var image = new Image();
image.height = 100;
image.title = file.name;
image.src = this.result;
image.className = "draggable";
image.draggable = true;
//draggable.js dependent of previewImage.js
aux.appendChild(div);
div.appendChild(image)
});
reader.readAsDataURL(file);
}
}
image.className = "draggable";
image.draggable = true;