【发布时间】:2010-10-30 18:49:18
【问题描述】:
我有一个 div 标签
<div id="theDiv">Where is the image?</div>
我想在 div 中添加一个图片标签
最终结果:
<div id="theDiv"><img id="theImg" src="theImg.png" />Where is the image?</div>
【问题讨论】:
标签: jquery
我有一个 div 标签
<div id="theDiv">Where is the image?</div>
我想在 div 中添加一个图片标签
最终结果:
<div id="theDiv"><img id="theImg" src="theImg.png" />Where is the image?</div>
【问题讨论】:
标签: jquery
您是否尝试过以下方法:
$('#theDiv').prepend('<img id="theImg" src="theImg.png" />')
【讨论】:
$("#theDiv").append("<img id='theImg' src='theImg.png'/>");
您需要阅读文档here。
【讨论】:
我的 2 美分:
$('#theDiv').prepend($('<img>',{id:'theImg',src:'theImg.png'}))
【讨论】:
如果我们想在函数image()被调用时改变<div>标签的内容,我们必须这样做:
Javascript
function image() {
var img = document.createElement("IMG");
img.src = "/images/img1.gif";
$('#image').html(img);
}
HTML
<div id="image"></div>
<div><a href="javascript:image();">First Image</a></div>
【讨论】:
除了 Manjeet Kumar 的帖子(他没有声明)
var image = document.createElement("IMG");
image.alt = "Alt information for image";
image.setAttribute('class', 'photo');
image.src="/images/abc.jpg";
$("#TheDiv").html(image);
【讨论】:
var img;
for (var i = 0; i < jQuery('.MulImage').length; i++) {
var imgsrc = jQuery('.MulImage')[i];
var CurrentImgSrc = imgsrc.src;
img = jQuery('<img class="dynamic" style="width:100%;">');
img.attr('src', CurrentImgSrc);
jQuery('.YourDivClass').append(img);
}
【讨论】: