我有一个从循环生成的可点击 div。我正在尝试对其进行设置,以便 onclick 将其自身附加到不同页面上的另一个 div 中。这可能吗。这是我尝试过的。
userDiv.addEventListener("click",
function () {
var one = document.getElementById('userDiv');
var two = document.getElementById('singleUser');
two.appendChild(one);
window.open("Createtask.html");
}
编辑这里是所有代码
var theID;
var theName;
var thePhoto;
var len = results.rows.length;
console.log(len);
for (var i = 0; i < len; i++) {
theID = results.rows.item(i).id;
console.log(theID);
theName = results.rows.item(i).username;
var htmlStr = theName;
console.log(theName);
thePhoto = results.rows.item(i).imagepath;
console.log(thePhoto);
var imageHold = new Image();
imageHold.src = thePhoto;
console.log("this is the src:" + imageHold.src);
var userDiv = document.createElement("div"); //Create the div
userDiv.innerHTML = htmlStr;
userDiv.appendChild(imageHold);
imageHold.width = 100;
imageHold.height = 100;
document.getElementById('showUsers').appendChild(userDiv); //append it to the document
userDiv.style.display = 'block';
identity = results.rows.item(i).username;
userDiv.id = identity;
console.log(identity);
userDiv.addEventListener("click",
function () {
var w = window.open("Createtask.html");
w.addEventListener("load", function(){
var one = document.getElementById('userDiv');
var two = w.document.getElementById('singleUser');
two.appendChild(one);
});
});
}
}