输入标签值属性的转义要求

发布于 2024-08-23 01:36:14 字数 367 浏览 4 评论 0原文

HTML 标记中输入标记的 value 属性中的字符转义有哪些要求?

只是双引号需要转义吗? HTML 也有特殊字符吗?

我尝试查看 W3C 规范,但找不到有关如何将内容放入 value 属性的任何具体细节。

我想不言而喻, " 应该转义为 " ,但是其他的呢?转义和不转义似乎在我所有的浏览器,但我不想选择不标准的浏览器并最终得到损坏的 HTML 或 &

What are the requirements for character escaping in the input tag's value attribute in the HTML markup?

Is it just double quotes that need to be escaped? HTML special characters as well?

I tried looking through the W3C spec, but I couldn't find any specific details on how stuff should be put into the value attribute.

I suppose it goes without saying that " should be escaped to ", but what about the others? Both escaping and not escaping seem to work just fine in all my browsers, but I don't want to pick the unstandard one and wind up with broken HTML or &.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔一刀 2024-08-30 01:36:14

属性值只需转义引号即可。

如果属性使用双引号,请将双引号替换为 "

如果您的属性包含在单引号内,请使用 ' 对字符串中的单引号进行转义。 注意:不要使用'

这些都是有效的 HTML:

<input value=foo>
<input value="foo">
<input value='foo'>

注意:引号不一定是有效的 HTML。然而 XHTML 确实需要引号。如果没有引号,属性值中不能有空格。

Attribute values only need to have their quote marks escaped.

If the attribute uses double quotes, replace double quotes with ".

If your attribute is contained within single quotes, escape the single quotes in the string with '. Note: don't use '!

These are all valid HTML:

<input value=foo>
<input value="foo">
<input value='foo'>

Note: quotes aren't required to be valid HTML. XHTML however does require quotes. Without quotes, you can't have spaces in the attribute value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文