简体   繁体   English

将HTML文本设置为textarea js

[英]Set html text into textarea js

I have a textarea where i need to set some html text 我有一个textarea,我需要设置一些html文本

<textarea id="text"></textarea>
<div id="elem">
  <p>text</p>
  <hr />
  <br />
  <p> some text</p>
</div>
..
$('#text').val($('#elem').html());

when I do this it's put text with html tags instead of parse it and put like a plain text. 当我这样做时,它会使用html标签而不是解析文本来放置文本,并像纯文本一样放置。 i also tried functions append(), html(), text() . 我也尝试过函数append(), html(), text() none of them helps me 他们都没有帮助我

text() worked for me. text()对我有用。 See https://jsfiddle.net/mgLp90em/ . 参见https://jsfiddle.net/mgLp90em/

What is the jQuery version you are using? 您正在使用什么jQuery版本?

This should do the trick, gets only text from the children within your #elem : 这应该可以解决问题,只从#elem的孩子那里获取文本:

$('#text').val($('#elem').children().text());

FIDDLE 小提琴

I'd recommend to use a contenteditable div element instead of a textarea as follows: 我建议使用contenteditable div元素而不是textarea ,如下所示:

<div class="text-area" contenteditable>
    <h1>Your content here</h1>
</div>

See JSfiddle demo 参见JSfiddle演示

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

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