javascript - 如何让jquery字符串中的标 checkout 现在html中

标签 javascript jquery html css

当我想在另一个元素中添加一个元素时,例如 <span><p> 里面使用 jquery,标签只是显示为一个字符串。我做了一个fiddle举个例子:

HTML:

<button>
Click Me
</button>

<p>
This will have a red thing right here: ___.
</p>

CSS:

span {
  color: red;
  font-weight: bold;
}

p {
  color: blue;
  font-weight: bold;
}

button {
  border: 2px solid blue;
  background-color: rgba(0, 0, 0, 0);
  padding: 10px;
  font-size: 15px;
  cursor: pointer;
  transition-duration: 0.3s;
}
button:hover {
  background-color: rgba(0, 0, 255, 0.3);
  color: white;
}

button:active {
  background-color: rgba(0, 0, 255, 0.6);

}

JS:

$(document).ready(function(){

$('button').click(function(){

$('p').text("This will have a red thing right here: <span> The red thing! </span>")

});

});

最佳答案

你应该使用 html() 而不是 text()

$('p').html("This will have a red thing right here: <span> The red thing! </span>")

https://jsfiddle.net/IA7medd/4cpno334/1/

关于javascript - 如何让jquery字符串中的标 checkout 现在html中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37392801/

相关文章:

javascript - 为什么这个 For 循环会使浏览器实验室崩溃?

html - 如何使用 flexbox 子元素创建包装

php - 检查用户是否通过电子邮件激活其帐户并在单击激活链接后显示消息 PHP/JavaScript

javascript - 动态插入分页符

使用历史对象的 Javascript 概念

jquery 从屏幕中心点缩放 div 的高度和宽度

jquery - $.event.mouseHooks 在 jquery 3 中被破坏,在测试版中工作

html - overflow css属性浏览器解释差异

html - 将CSS中的事件状态添加到导航栏

javascript - 设置 xlink :href in svg with angularjs to base64-encoded image