我试图找出一些没有结果的东西。我有一个用作按钮的简单<img>
,如果我单击它,来自其他<h3>
元素的文本应该会更改。该部分正在工作,但我也试图在新文本中包含图像作为 Bullet 元素,但它不起作用,它也呈现为文本。这是我的实际代码:
.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');
});
你能帮我找到解决方案吗?
使用 .html()
而不是 .text()
,因为.text()
只会设置元素的textContent
,它不会插入提供的参数作为DOM Element
$("#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');
});
<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>