gpt4 book ai didi

javascript - 从函数参数中获取图像名称

转载 作者:行者123 更新时间:2023-11-30 19:40:42 24 4
gpt4 key购买 nike

我需要将图像嵌入到元素中。例如,在下面的代码中,图像 dog.png 将被插入到 #board div 中。我该怎么做?

HTML

<div id="board"></div>

JavaScript

function show_image(symbol, value, identificator) {
var img = document.createElement("IMG");
img.src = symbol.png;
identificator.appendChild(img);
}

show_image("dog", "15", "board");

最佳答案

只需使用 getElementByIdappendChild。另请注意,您需要连接 .png 字符串 - 您试图访问不存在的 "dog".png:

function show_image(symbol, value, identificator) {
var img = document.createElement("IMG");
img.src = symbol + ".png";
document.getElementById(identificator).appendChild(img);
}

show_image("dog", "15", "board");
<div id="board"></div>

关于javascript - 从函数参数中获取图像名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55435916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com