javascript - How to write a code inside <pre> tag without apply the code - Stack Overflow

I have two places, the first is,<textarea><textarea> and used to write the code.The secon

I have two places, the first is,<textarea></textarea> and used to write the code.
The second is, <pre></pre> and used to display the code.
The problem is, when write a code in the text area to display it in the second place, the code is applied and displayed as HTML element not just code as I want.

The wrong behavior:

The correct behavior (What I want):

The code:

<textarea id="inputCode" cols="50" rows="10"></textarea><br/>
<pre id="outputCode"></pre>

I have two places, the first is,<textarea></textarea> and used to write the code.
The second is, <pre></pre> and used to display the code.
The problem is, when write a code in the text area to display it in the second place, the code is applied and displayed as HTML element not just code as I want.

The wrong behavior:

The correct behavior (What I want):

The code:

<textarea id="inputCode" cols="50" rows="10"></textarea><br/>
<pre id="outputCode"></pre>
Share Improve this question edited Aug 7, 2015 at 1:46 Lion King asked Aug 7, 2015 at 1:29 Lion KingLion King 33.8k26 gold badges90 silver badges157 bronze badges 2
  • Something like this? jsfiddle/h175y2vx – Rick Hitchcock Commented Aug 7, 2015 at 1:38
  • @RickHitchcock: Yes, like that. – Lion King Commented Aug 7, 2015 at 1:42
Add a ment  | 

1 Answer 1

Reset to default 9

Using innerText to set the value of the second textarea will add the textual value only, not the HTML.

<pre id="code"></pre>

document.getElementById("code").innerText = "<input type='text' />";

Will insert in to the pre tags,

<input type='text' />

Example

JSFiddle

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742343786a4426137.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信