我需要像这样添加一个 block 。
<td valign="bottom" width="25%">
<a href="/images/00000.jpg">
<img width="150" height="100" src="http://farm4.static.flickr.com/3114/2524849923_1c191ef42e.jpg" border="0" alt="Click" title="Click"></a>
</td>
我正在尝试类似的方法。但是这里的 td 元素没有附加图像元素。
var img = document.createElement("img");
img.src = src;
img.width = width;
img.height = height;
img.alt = alt;
img.onclick=function () { alert(src); };
td=td.appendChild(img);
alert(td);
document.body.appendChild(td);
最佳答案
这应该可行(它创建了所提到的结构):
td = document.createElement('td');
td.valign = 'bottom';
td.width = '25%';
a = document.createElement('a');
a.href = '/images/00000.jpg';
img = document.createElement('img');
img.width = '150';
img.height = '100';
img.border = '0';
img.alt = img.title = 'Click';
img.src = 'http://farm4.static.flickr.com/3114/2524849923_1c191ef42e.jpg';
a.appendChild(img);
td.appendChild(a);
document.body.appendChild(td);
关于javascript - 如何将 HTML block 动态添加到页面中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8960471/