附加图像到 div 未显示

我试图通过创建一个新的 div 来打印 img,然后将其附加到图像,但图像没有显示。

JS 这里 rpsGame 是主函数,调用了 rpsFrontEnd 函数,并且 rpsFrontEnd 的参数是正确的。


function rpsGame(yourChoice) {



    var humanChoice = yourChoice.id;

    var botChoice = noToChoice(randTorpsInt());  

    console.log("bot choice " + botChoice);

    console.log("your choice " + humanChoice);


    var results = decideWinner(humanChoice, botChoice);

    console.log(results);

    var message = finalMessage(results);

    console.log(message);

    rpsFrontEnd(humanChoice, botChoice, message);


}

这个 rpsFrontend 首先删除所有图像,然后创建一个新的 div 并在其中附加一个 img,但 img 没有被打印


function rpsFrontEnd(humanImageChoice, botImageChoice, finalMessage) {

    var imageDatabase = {

        "rock": document.getElementById("rock").src,

        "paper": document.getElementById("paper").src,

        "scissor": document.getElementById("scissor").src

    }

    document.getElementById("rock").remove();

    document.getElementById("paper").remove();

    document.getElementById("scissor").remove();


    var humanDiv = document.createElement("div");

    var botDiv = document.createElement("div");

    var messageDiv = document.createElement("div");

    


    humanDiv.innerHTML="<img src'"+imageDatabase[humanImageChoice]+"'>";

    console.log(humanDiv);

    document.getElementById("col").appendChild(humanDiv);


}

谢谢你的时间!


森林海
浏览 43回答 2
2回答

哈士奇WWW

一小时后我得到答案非常小的错误humanDiv.innerHTML="<img&nbsp;src='"+imageDatabase[humanImageChoice]+"'>";再次感谢那些在这里付出时间的人,innerHtml 中再次缺少等号

小怪兽爱吃肉

document.getElementById("row").appendChild(humanDiv);此行不起作用,因为 HTML 中没有包含 ID 行的元素 - 它是一个类,因此 getElementById 找不到它。改变这个<div&nbsp;class="row">对此<div&nbsp;id="row">
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript