反应文本区域不显示新行

react textarea doesn't show new lines

我有一个问题,我希望你知道如何帮助我。

我有一个 textarea,我可以在其中显示来自后端的回复。

响应格式如下:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:j.0="http://xmlns.com/foaf/0.1/">
  <j.0:Person rdf:about="https://create-profile.com">
    <j.0:homepage></j.0:homepage>
    <j.0:firstName></j.0:firstName>
    <j.0:based_near></j.0:based_near>
    <j.0:mbox_sha1sum>myemail-example@s.com</j.0:mbox_sha1sum>
    <j.0:lastName></j.0:lastName>
    <j.0:img></j.0:img>
    <j.0:schoolHomepage></j.0:schoolHomepage>
    <j.0:title></j.0:title>
    <j.0:skypeID></j.0:skypeID>
    <j.0:currentProject></j.0:currentProject>
    <j.0:publications></j.0:publications>
    <j.0:nick></j.0:nick>
    <j.0:weblog></j.0:weblog>
    <j.0:account>https://create-profile.com</j.0:account>
    <j.0:account></j.0:account>
    <j.0:workplaceHomepage></j.0:workplaceHomepage>
  </j.0:Person>
</rdf:RDF>

但是,当我把它放到文本区域时,我得到了这样的东西:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns# xmlns:j.0="http://xmlns.com/foaf/0.1/"><j.0:Person rdf:about="https://create-profile.com"> <j.0:homepage></j.0:homepage>

我尝试用 css、新行等做一些事情,但它不起作用。只有当我将响应发送到 div.

时它才有效

谁能告诉我如何在文本区域中使用相同格式的响应?

很难从此处发布的数据中看出哪些字符代码分隔了您的标签。从示例输出看来,它可能只是 spaces。在这种情况下,您可以尝试下面的代码。如果它不是 space 并且您可以确定分隔符是什么(例如 '\r'),则可以用它替换正则表达式中的 space 字符。

// Example data with space as a delimiter
const data = `<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:j.0="http://xmlns.com/foaf/0.1/"> <j.0:Person rdf:about="https://create-profile.com"> <j.0:homepage></j.0:homepage> <j.0:firstName></j.0:firstName> <j.0:based_near></j.0:based_near> <j.0:mbox_sha1sum>myemail-example@s.com</j.0:mbox_sha1sum> <j.0:lastName></j.0:lastName> <j.0:img></j.0:img> <j.0:schoolHomepage></j.0:schoolHomepage> <j.0:title></j.0:title> <j.0:skypeID></j.0:skypeID> <j.0:currentProject></j.0:currentProject> <j.0:publications></j.0:publications> <j.0:nick></j.0:nick> <j.0:weblog></j.0:weblog> <j.0:account>https://create-profile.com</j.0:account> <j.0:account></j.0:account> <j.0:workplaceHomepage></j.0:workplaceHomepage> </j.0:Person> </rdf:RDF>`;

var regex = /> /g;
const formattedData = data.replace(regex,'>\n');;
document.getElementById('xml-data').innerHTML = formattedData;
<html>
  <head></head>
  <body>
    <form>
      <textarea id="xml-data"></textarea>
    </form>
  </body>
</html>