我需要将图像嵌入到元素中。例如,在下面的代码中,图像 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");
最佳答案
只需使用 getElementById
和 appendChild
。另请注意,您需要连接 .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/