为什么 select 标签至少需要一个子标签才能成为 xhtml 1.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设这是一个问题的原因是因为您动态添加
选项
,我建议也动态添加select
。事实上,您可以在页面加载时添加一个
node
...并将其替换为必要时
选择
和选项
。Assuming that the reason this is an issue is because you are adding the
option
s dynamically, I would suggest that also add theselect
dynamically, too.In fact, you could add a
node
on page load...<a name="whatever" id="whatever-id" />
and replace it with a
select
andoptions
when necessary.没有任何选项的选择的语义是什么?你可能还没有选择...
What would be the semantic meaning of a select without any options? You might as well not have a select...
为什么?很可能是因为它是大约 10 年前定义的标准。
您应该考虑继续...
下面的示例。
HTML5
将您的文档类型行切换为:
将元字符集行切换为:
生成的 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:
And your meta charset line to:
Resulting HTML Markup:
Feel free to validate in http://validator.w3.org/#validate_by_input