使用jquery将图像插入到div

使用jquery将图像插入到div,jquery,Jquery,我喜欢使用jquery向div添加文本和一些图像 我可以这样做来添加文本: <script type="text/javascript"> $(document).ready(function () { $("#help").text('This is the description').css({"border": "1px solid", "color": "Black", "font-size": "14px", "font-weight": "bold", "paddin

我喜欢使用jquery向div添加文本和一些图像

我可以这样做来添加文本:

<script type="text/javascript">
$(document).ready(function () { 

$("#help").text('This is the description').css({"border": "1px solid", "color": "Black", "font-size": "14px", "font-weight": "bold", "padding": "30px 0 30px 0", "width": "auto", "height": "auto", "text-align": "left", "background": "lightyellow"});

$(文档).ready(函数(){
$(“#帮助”).text('这是描述').css({“边框”:“1px实心”、“颜色”:“黑色”、“字体大小”:“14px”、“字体重量”:“粗体”、“填充”:“30px 0 30px 0”、“宽度”:“自动”、“高度”:“自动”、“文本对齐”:“左侧”、“背景”:“浅黄色”);
我试过这个:

$("#help").append('<img src="description.png" alt="box_plot_description" />'); 

});
$(“#帮助”).append(“”);
});
如何使用jquery将图像附加到此文本

我已经在文本之后添加了append,但是append正在覆盖div。我喜欢在div的开头添加文本,然后添加图像。

使用
html()
方法

$("#help").html('<img src="somesrc.jpg" alt="someimage" />');
$("#help").text("Some Text");
$("#help").append('<img src="somesrc.jpg" alt="someimage" />');
样本:

您也可以使用该方法

var img=$("<img />").attr("src","someimag.jpg");
img.appendTo($("#help"));
var img=$(“
示例:

使用
html()
方法

$("#help").html('<img src="somesrc.jpg" alt="someimage" />');
$("#help").text("Some Text");
$("#help").append('<img src="somesrc.jpg" alt="someimage" />');
样本:

您也可以使用该方法

var img=$("<img />").attr("src","someimag.jpg");
img.appendTo($("#help"));
var img=$(“
示例:

如果要附加:

$('#divid').append('<img src="path" />');
$('#divid')。追加('');
如果您只想设置特定的图像:

$('#divid').html('<img src="path" />');
$('#divid').html('');
如果要附加:

$('#divid').append('<img src="path" />');
$('#divid')。追加('');
如果您只想设置特定的图像:

$('#divid').html('<img src="path" />');
$('#divid').html('');

您可以使用
.html()

$(“#help”).html(“”);

您可以使用
.html()

$(“#help”).html(“”);

请参见@Shyju的答案。您需要使用.html()而不是.text()。请参见@Shyju的答案。您需要使用.html()而不是.text()谢谢你的回复。这覆盖了我的div,我想附加到我的div中。基本上,我希望在文本中添加一些描述,然后添加一个图像。谢谢你的回复。这覆盖了我的div,我想附加到我的div中。基本上,我希望在文本中添加一些描述,然后添加一个图像。Everlof,我已经更新了代码。当我这样做的时候在文本后追加,它会覆盖我的div。我再也看不到div中的文本了。这取决于你希望它看起来如何?这个示例使用p标记作为文本Everlof,我已经更新了代码。当我在文本后追加时,它会覆盖我的div。我再也看不到div中的文本了。这取决于你希望它看起来如何?这个示例使用p文本的标记