我有一个div标签
<div id="theDiv">Where is the image?</div>
我想在div
中添加一个图片标签最终结果:
<div id="theDiv"><img id="theImg" src="theImg.png" />Where is the image?</div>
答案 0 :(得分:294)
您是否尝试过以下操作:
$('#theDiv').prepend('<img id="theImg" src="theImg.png" />')
答案 1 :(得分:48)
我的2美分:
$('#theDiv').prepend($('<img>',{id:'theImg',src:'theImg.png'}))
答案 2 :(得分:22)
$("#theDiv").append("<img id='theImg' src='theImg.png'/>");
您需要阅读文档here。
答案 3 :(得分:10)
如果我们想在调用函数<div>
时更改image()
标记的内容,我们必须这样做:
<强>的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>
答案 4 :(得分:4)
In addition to Manjeet Kumar's post (he didn't have the declaration)
var image = document.createElement("IMG");
image.alt = "Alt information for image";
image.setAttribute('class', 'photo');
image.src="/images/abc.jpg";
$(#TheDiv).html(image);
答案 5 :(得分:1)
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);
}