可以在文本区域字段中添加换行符,同时保持 XHTML 1.0 有效吗?

发布于 2024-07-16 22:23:40 字数 1141 浏览 5 评论 0 原文

编辑:重新阅读我的帖子后,我想我有点不清楚问题是什么。 让我尝试重新表述一下:

用户可以使用表单中的文本区域字段在我的网站上留下评论。 应该可以使用
在注释中添加换行符。 然后,评论作为字符串存储在 mysql 数据库中(转义以确保安全),稍后从数据库中检索并显示在网站上。 显示注释时无法识别常规换行符。

用户可以编辑他们的评论,在这种情况下,原始评论将显示在要修改的文本区域字段中(见下图)。 问题是,如果存在任何
,则该代码作为 XHTML 1.0 无效(请参阅下面的错误消息)。

我可以使用任何其他类型的换行符使代码有效吗? 正如评论中所述,常规换行符与 xml:space="preserve" 结合使用不起作用(换行符显示在文本区域字段中,但当评论在网站上显示为普通文本时则不会显示)。


原始问题:

我有一个网络表单,其中包含一个用于留下评论的文本区域字段,我希望它能够使用
添加换行符。 下面是一个简单的示例:


(来源:shipit.se

它有效正如预期的那样,但是在使用 W3C 验证服务时,它不会验证为 XHTML 1.0 过渡。 这是我得到的错误:

  1. 错误第 90 行,第 1587 列:文档类型不允许此处使用元素“br”

…ription" rows="0" cols="0">测试
换行符

有没有办法使换行符有效的代码,或者我是否需要找到解决方法(例如,在文本区域字段中使用特定标签作为换行符,然后将其替换为
稍后显示评论时)?

EDIT: After re-reading my post I think I am being a little bit unclear about what the problem is. Let me try to re-phrase it:

Users can leave comments on my site using a textarea field in a form. It should be possible to add line breaks in the comment using <br />. The comment is then stored as a string in a mysql-database (escaped to make it safe) and later on retrieved from the database and displayed on the site. Regular line breaks are not recognized when the comment is displayed.

Users can edit their comments, and in that case the original comment is displayed in a textarea field to be modified (see image below). The problem is that if there are any <br /> present, the code is not valid as XHTML 1.0 (see error message below).

Can I make the code valid using any other type of line break? As stated in the comments, regular line breaks in combination with xml:space="preserve" does not work (line breaks are displayed in the textarea field, but not when the comment is displayed as normal text on the site).


Original question:

I have a web-form which includes a textarea field to leave comments, and I want it to be able to add line breaks using <br />. Below is a simple example:

screen dump
(source: shipit.se)

It works as intended, however it does not validate as XHTML 1.0 transitional when using the W3C validation service. This is the error I get:

  1. Error Line 90, Column 1587: document type does not allow element "br" here

…ription" rows="0" cols="0">Test<br />line break</textarea></dd><dt class="cha

The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).

One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error).

Is there a way to make the line breaks valid code, or do I need to find a workaround (like e.g. using specific tags for line breaks in the textarea field and then replacing them with <br /> later on when displaying the comment)?

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

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

发布评论

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

评论(4

玉环 2024-07-23 22:23:40

您是否尝试过添加 xml:space="preserve" 属性并仅使用普通的换行符?

Have you tried adding a xml:space="preserve" attribute and use just plain normal line breaks?

当爱已成负担 2024-07-23 22:23:40

为什么不使用常规换行符? 这些工作得很好。

如果您希望出现
,则需要对这些实体进行编码:

<br />

Why not use regular linebreaks? Those work just fine.

If you want <br /> to appear, you need to encode those entities:

<br />
这个俗人 2024-07-23 22:23:40

来自 DTD

在页面的 XHTML 代码中,

你的问题似乎表明了两件不同的事情。 该图像显示“
”写入

在第一种情况下,通过使用适当的实体在文本区域中向用户显示“Test
换行符
”,就像 altCognito写道:

<textarea>Test<br />line break</textarea>

用户输入的任何内容,然后在新页面上的

如果要显示用户的条目:

测试
换行符

...as...

测试
换行符

...在另一个文本区域内,那么您需要解析原始

From the DTD:

<!ELEMENT TEXTAREA - - (#PCDATA) -- multi-line text field -->

In the XHTML code of your page, a <textarea> can only contain text (#PCDATA), and you cannot nest any XHTML elements within the <textarea>.

Your questions seems to show two different things. The image shows "<br />" written into a <textarea>, while the message from the W3C Validator is referring to a <br /> element written into the XHTML of your page within the <textarea> element.

In the first case, having "Test<br />line break" appear to the user in the text area is done by using the appropriate entities, just as altCognito wrote:

<textarea>Test<br />line break</textarea>

Anything that's being entered by the user that is then redisplayed within a <textarea> on a new page should be encoded (i.e., use entities for &, ", ', <, and >).

If want to display a user's entry:

Test<br />line break

...as...

Test
line break

...within another text area, then you will need to parse what has been entered into the original <textarea> and replace the user-entered <br />s with normal line breaks. See Lucero's answer.

听不够的曲调 2024-07-23 22:23:40

我刚刚做了这个并且有效。

<textarea name="contenidoMensaje" id="contenidoMensaje" cols="80" rows="10">
-----Mensaje Original-----
{blank space}
<?php echo str_replace("<br />", "\n", $contenidoMensaje); ?>
</textarea>

i just did this and works.

<textarea name="contenidoMensaje" id="contenidoMensaje" cols="80" rows="10">
-----Mensaje Original-----
{blank space}
<?php echo str_replace("<br />", "\n", $contenidoMensaje); ?>
</textarea>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文