简体   繁体   English

如何获取jQuery字符串中的标记以显示在HTML中

[英]How to get tags in a jquery string to appear in html

When I want to add an element inside of another element, for example, a <span> inside of a <p> using jquery, the tags just appear as a string. 当我想使用jquery在另一个元素内添加一个元素,例如<p>内的<span> ,标签只是显示为字符串。 I made a fiddle as an example: 我以小提琴为例:

HTML: HTML:

<button>
Click Me
</button>

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

CSS: 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: JS:

$(document).ready(function(){

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

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

});

});

只需将.text()更改为.html()

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

You should use html() instead of text() 您应该使用html()而不是text()

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM