我正在尝试添加图像并执行与下面的代码等效的操作,但是除了创建textnode之外,我将如何创建图像元素?
//create the DOM object
var newSpan = document.createElement('span');
// add the class to the 'span'
newSpan.setAttribute('class', 'ABC');
document.getElementById('text').appendChild(newSpan);
var selectedTextNode = document.createTextNode();
newSpan.appendChild(selectedTextNode);
最佳答案
<div id="text"></div>
//create the DOM object
var newSpan = document.createElement('span');
// add the class to the 'span'
newSpan.setAttribute('class', 'ABC');
document.getElementById('text').appendChild(newSpan);
var image = document.createElement("img");
image.src = "http://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/Fairfax-harrison-1913.jpg/100px-Fairfax-harrison-1913.jpg"
newSpan.appendChild(image);
在jsfiddle
关于javascript - 如何将图像添加到span子元素?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16123710/