我有一个div标签
<div id="theDiv">Where is the image?</div>
我想在div中添加一个图像标签
最终结果:
<div id="theDiv"><img id="theImg" src="theImg.png" />Where is the image?</div>
您是否尝试过以下方法:
$('#theDiv').prepend('<img id="theImg" src="theImg.png" />')
我的2美分:
$('#theDiv').prepend($('<img>',{id:'theImg',src:'theImg.png'}))
$("#theDiv").append("<img id='theImg' src='theImg.png'/>");
您需要阅读文档here。
如果我们想在调用<div>
is函数时更改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>
除了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);
}