使用功能更改文本并包含项目符号图像

时间:2016-03-21 17:29:26

标签: javascript jquery html

我试图找出没有结果的东西。我有一个简单的<img>用作按钮,如果我点击它,其他<h3>元素的文本应该会改变。这部分是有效的,但我也试图在新文本中包含一个图像作为子弹元素,但它不起作用,它也像文本一样呈现。这是我的实际代码:

HTML:

 <img id="button1" src="images/info.png"/>
 <h3 class="text1"> Here's some temporal text </h3>

JAVASCRIPT:

 $( "#button1").click(function() {
 $(".text1").text('This will be the new text also including a bullet <img src="images/bullet.png" style="margin-right:10px;"/> And here continues the text, but seems to be that the bullet is not being rendered:( only as text');
 });

你能帮我找一个解决方案吗?

1 个答案:

答案 0 :(得分:3)

使用.html()代替.text()因为.text()只会设置元素的textContent,所以不会将提供的参数插入DOM Element

&#13;
&#13;
 $("#button1").click(function() {
   $(".text1").html('This will be the new text also including a bullet <img src="images/bullet.png" style="margin-right:10px;"/> And here continues the text, but seems to be that the bullet is not being rendered:( only as text');
 });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<img id="button1" src="images/info.png" />
<h3 class="text1"> Here's some temporal text </h3>
&#13;
&#13;
&#13;