Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中正确创建img元素,而不使其无法识别图像并读取;“对象HTML图像”;代替形象 我哪里出错了?我试图在javascript中创建一个img元素,嵌套在一个button元素中,并将onmouseover和onmouseout设置为img元素,当检测到事件时,img元素将更改图像。_Javascript_Image - Fatal编程技术网

如何在javascript中正确创建img元素,而不使其无法识别图像并读取;“对象HTML图像”;代替形象 我哪里出错了?我试图在javascript中创建一个img元素,嵌套在一个button元素中,并将onmouseover和onmouseout设置为img元素,当检测到事件时,img元素将更改图像。

如何在javascript中正确创建img元素,而不使其无法识别图像并读取;“对象HTML图像”;代替形象 我哪里出错了?我试图在javascript中创建一个img元素,嵌套在一个button元素中,并将onmouseover和onmouseout设置为img元素,当检测到事件时,img元素将更改图像。,javascript,image,Javascript,Image,这是我的Javascript(ps,我知道我多次重复了一些函数,但我不知道如何正确地简化它们) 我还尝试用两种不同的方式创建img属性 moveImg = document.createElement("img"); removeImg.innerHTML = "< img src='icons/trash.png' id='dormant' onmouseover='newTrashPicture()' onmouseout='oldTrashPicture()'/>"

这是我的Javascript(ps,我知道我多次重复了一些函数,但我不知道如何正确地简化它们) 我还尝试用两种不同的方式创建img属性

moveImg = document.createElement("img");
    removeImg.innerHTML = "< img src='icons/trash.png' id='dormant' onmouseover='newTrashPicture()' onmouseout='oldTrashPicture()'/>"

var createImg = document.createElement("img");
createImg.src="icons/check.png";
createImg.setAttribute("id" , "dormant2");
createImg.onmouseover = newCheckPicture();
createImg.onmouseout = oldCheckPicture();


document.getElementById('add').addEventListener('click',function() {
var value = document.getElementById('item').value;
If(value) addItemTodo(value); {

}
});

function addItemTodo(text) {
var list = document.getElementById('todo');

var item = document.createElement("li");
item.innerText = text;

var buttons = document.createElement('div');
buttons.classList.add('buttons');

var removePic = document.createElement('button');
removePic.classList.add('remove');
removePic.innerHTML = removeImg;

var complete = document.createElement('button');
complete.classList.add('complete');
complete.innerHTML = createImg;

buttons.appendChild(removePic);
buttons.appendChild(complete);
item.appendChild(buttons);
list.appendChild(item);
}


function newTrashPicture() {
document.getElementById("dormant").src="icons/redtrash.png";
}

function oldTrashPicture() {
document.getElementById("dormant").src="icons/trash.png";
}

function newCheckPicture() {
document.getElementById("dormant2").src="icons/greencirclecheck.png";
}

function oldCheckPicture() {
document.getElementById("dormant2").src="icons/check.png";

您可以做的是将图像附加到按钮元素,而不是将其设置为innerHTML属性

替换:

removePic.innerHTML = removeImg;
与:

removePic.appendChild(removeImg);
与“完成”按钮相同

innerHTML属性接受字符串作为参数,但您正在向它传递一个对象

这不起作用:

var removeImg = document.createElement("img");
removePic.innerHTML = removeImg;
但这确实:

var removeImg = "<img src='icons/trash.png' id='dormant'>";
removePic.innerHTML = removeImg;
var removeImg=“”;
removePic.innerHTML=removeImg;
所以要么你:

  • 附加对象
  • 使用字符串设置innerHTML
var removeImg = document.createElement("img");
removePic.innerHTML = removeImg;
var removeImg = "<img src='icons/trash.png' id='dormant'>";
removePic.innerHTML = removeImg;