为什么< textarea>显示<>而不是& lt;& gt; [英] Why does <textarea> display <> instead of <>?

查看:1991
本文介绍了为什么< textarea>显示<>而不是& lt;& gt;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览器的textarea是否应显示原始文字?查看,并考虑 HTML中的CDATA段(查看

脚本(和 style )元素具有这样的CDATA标识,而 textarea 存储数据(unes​​caped),但确保HTML编码在CDATA部分之外的HTML(甚至在内部,注意防止终止符如< / anything - < / 字符必须分开以在HTML中完全合法。请参阅 CDATA vs PCDATA 了解详情。



>

Shouldn't a browser's textarea display raw text? Look at the following snaps from this post, and pay attention to the <script> tag:

1- We can see the angled brackets around the script word:

2- Now look at the source of the page. We can see the angled brackets are represented by their HTML entities:

3- Click to edit the post and you'll see that the angled brackets are visible in the textarea--NOT their HTML entities:

4- Look at the XHR response from the server (when we clicked edit), we can see the HTML entities and NOT the angled brackets we see in the textarea:

How is it possible that the textarea--when editing the post--displays the angled brackets and not their HTML entities? Shouldn't textareas display raw text? I tried this on my site, and the textarea shows the HTML entities, and not brackets, with identical source code.

The post page also shows the angled brackets even when they are inside code and pre tags. Shouldn't they be output as raw text too? I also tried this on my site, and the page shows the HTML entities, and not the angled brackets.

What I am missing here?

I'm asking because struggling with implementing a feature on my site. I want to show people the encoded entities when they go to edit something, but I want to store a raw version. SO seems to have done, but I have no idea how.

解决方案

See Character Data and Markup and consider CDATA sections in HTML (take a peek at the DTD although do note it is insufficient to entirely describe HTML).

The script (and style) elements have such a CDATA designation while textarea does not. Store the data as it ("unescaped") but make sure to HTML-encoded outside of CDATA sections in the HTML (and even inside, take care to guard against terminators like </anything -- the < and / characters must be separated to be entirely legal in HTML). See CDATA vs PCDATA for some more details.

Happy coding.

这篇关于为什么&lt; textarea&gt;显示&lt;&gt;而不是&amp; lt;&amp; gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆