为什么 select 标签至少需要一个子标签才能成为 xhtml 1.0 有效

发布于 2024-12-01 05:14:41 字数 1268 浏览 0 评论 0原文

为什么 select 标签至少需要有一个子标签才能有效 xhtml 1.0 ?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Why select tag need at least one child to be xhtml valid ?</title>
</head>
<body>
Allowed users :<br/>
<select size="5" style="width: 200px">
</select>
<br />
Alls users :<br/>
<select size="5" style="width: 200px">
  <option>root</option>
  <option>John Doe</option>
  <option>Jane Doe</option>
</select>
</body>
</html>

为了有效,我需要写:

<select><option/></select>

我认为以编程方式添加空选项标记是无用的,而且很麻烦(特别是在 ASP.NET WebForms 中,我使用带有 dataBind 的 DropDownList 或 ListBox 控件):

if count == 0 then add <option/>...

DTD 中没有解释:http://www.w3.org/TR/xhtml1/dtds.html

你知道为什么 W3C 验证需要这样做吗?

(如何使用 ASP.NET WebForms 4.0 中的 DropDownList 或 ListBox 控件使该页面有效?)

Why the select tag need to have at least on child to be valid xhtml 1.0 ?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Why select tag need at least one child to be xhtml valid ?</title>
</head>
<body>
Allowed users :<br/>
<select size="5" style="width: 200px">
</select>
<br />
Alls users :<br/>
<select size="5" style="width: 200px">
  <option>root</option>
  <option>John Doe</option>
  <option>Jane Doe</option>
</select>
</body>
</html>

To be valid, I need to write :

<select><option/></select>

I think it's useless and it's cumbersome to add the empty option tag programmatically (especially in ASP.NET WebForms, I use a DropDownList or ListBox control with a dataBind) :

if count == 0 then add <option/>...

There is no explanation in the DTD : http://www.w3.org/TR/xhtml1/dtds.html

Do you know why W3C validation require that ?

(And how can I make this page valid using a DropDownList or ListBox control in ASP.NET WebForms 4.0 ?)

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

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

发布评论

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

评论(3

云仙小弟 2024-12-08 05:14:41

假设这是一个问题的原因是因为您动态添加选项,我建议也动态添加select

事实上,您可以在页面加载时添加一个 node...

并将其替换为必要时选择选项

Assuming that the reason this is an issue is because you are adding the options dynamically, I would suggest that also add the select dynamically, too.

In fact, you could add a node on page load...

<a name="whatever" id="whatever-id" />

and replace it with a select and options when necessary.

末蓝 2024-12-08 05:14:41

没有任何选项的选择的语义是什么?你可能还没有选择...

What would be the semantic meaning of a select without any options? You might as well not have a select...

忘你却要生生世世 2024-12-08 05:14:41

为什么?很可能是因为它是大约 10 年前定义的标准。
您应该考虑继续...

下面的示例。


HTML5

将您的文档类型行切换为:

<!DOCTYPE html>

将元字符集行切换为:

<meta charset="UTF-8" />

生成的 HTML 标记:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8" />
    <title>Why select tag need at least one child to be xhtml valid ?</title>
</head>
<body>
    Hello World : <select></select>
</body>
</html>

随时在 http://validator.w3.org/#validate_by_input

Why? Most likely because its a standard defined approximately 10 years ago.
You should consider moving on...

Example below.


HTML5

Switch your doctype line to:

<!DOCTYPE html>

And your meta charset line to:

<meta charset="UTF-8" />

Resulting HTML Markup:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8" />
    <title>Why select tag need at least one child to be xhtml valid ?</title>
</head>
<body>
    Hello World : <select></select>
</body>
</html>

Feel free to validate in http://validator.w3.org/#validate_by_input

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