简体   繁体   English

如何在div中添加img元素

[英]How to add an img element in div

I try to dynamically create an 'img' element and insert it into an existing div, but failed. 我尝试动态创建一个'img'元素并将其插入到现有的div中,但失败了。 Does any one know how to do this with prototype framework? 有没有人知道如何用原型框架做到这一点? Below is what I've tried: 以下是我尝试过的内容:

Case1: All html is fine. 案例1:所有的HTML都没问题。

<!-- html -->
<div id="thumbnailFrame" class="thumbnailFrameDiv">
    <img src="image.png" /> 
</div>

Case2: No image show up. 案例2:没有图像显示。

<!-- html -->
<div id="thumbnailFrame" class="thumbnailFrameDiv"> 
</div>

<!-- JS -->
var eleImg = new Element('img');
$('thumbnailFrame').insert(eleImg, {src:"image.png"});

I note you're using Prototype. 我注意到你正在使用Prototype。 You can do that using Element#update , which will replace all of the contents of the div with what you give it: 你可以使用Element#update做到这一点,它将用你给出的内容替换div所有内容:

$("thumbnailFrame").update('<img src="image.png"></img>');

...or with Element#insert , which will add to the content: ...或使用Element#insert ,它将添加到内容中:

// At the bottom (default)
$("thumbnailFrame").insert('<img src="image.png"></img>');

// At the top
$("thumbnailFrame").insert({top: '<img src="image.png"></img>'});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM