简体   繁体   English

javascript 变量 parse(parse) 中的 html 标记<p>标签)

[英]html tag within javascript variable parse(parse <p> tag)

var str2 = "<p>Apple apple ball</p>";
document.querySelector('textarea[placeholder="description"]').value = str2;

How parse the paragraph tag, is it possible?如何解析段落标签,有可能吗?

Your problem is that the variable is a string, even with the paragraph tag included.您的问题是该变量是一个字符串,即使包含段落标记也是如此。

So you'd first you could create an element to then get the value of it by adding the string to it as html:因此,您首先可以创建一个元素,然后通过将字符串添加为 html 来获取它的值:

var str2 = "<p>Apple apple ball</p>";
var tempElem = document.createElement('div');
tempElem.innerHTML = str2;
console.log(tempElem.innerText);

Then you can get the value of the tempElem through innerText .然后就可以通过innerText获取到tempElem的值。

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

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