为了账号安全,请及时绑定邮箱和手机立即绑定

附加图像到 div 未显示

附加图像到 div 未显示

森林海 2024-01-18 15:44:33
我试图通过创建一个新的 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);}谢谢你的时间!
查看完整描述

2 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

一小时后我得到答案非常小的错误

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

再次感谢那些在这里付出时间的人,innerHtml 中再次缺少等号


查看完整回答
反对 回复 2024-01-18
?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

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

此行不起作用,因为 HTML 中没有包含 ID 行的元素 - 它是一个类,因此 getElementById 找不到它。

改变这个

<div class="row">

对此

<div id="row">


查看完整回答
反对 回复 2024-01-18
  • 2 回答
  • 0 关注
  • 35 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信