jQuery jQuery在div标签内添加图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/941206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery add image inside of div tag
提问by Phill Pafford
I have a div tag
我有一个 div 标签
<div id="theDiv">Where is the image?</div>
I would like to add an image tag inside of the div
我想在 div 内添加一个图像标签
End result:
最终结果:
<div id="theDiv"><img id="theImg" src="theImg.png" />Where is the image?</div>
回答by Topher Fangio
Have you tried the following:
您是否尝试过以下操作:
$('#theDiv').prepend('<img id="theImg" src="theImg.png" />')
回答by Kuf
my 2 cents:
我的 2 美分:
$('#theDiv').prepend($('<img>',{id:'theImg',src:'theImg.png'}))
回答by jacobangel
回答by Manjeet Kumar
If we want to change the content of <div>
tag whenever the function image()
is called, we have to do like this:
如果我们想<div>
在函数image()
被调用时改变标签的内容,我们必须这样做:
Javascript
Javascript
function image() {
var img = document.createElement("IMG");
img.src = "/images/img1.gif";
$('#image').html(img);
}
HTML
HTML
<div id="image"></div>
<div><a href="javascript:image();">First Image</a></div>
回答by Evan Parsons
In addition to Manjeet Kumar's post (he didn't have the declaration)
除了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);
回答by Anand rao
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);
}