Html 是否允许列表内的 div?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1915077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is div inside list allowed?
提问by Kenan
I know that DIV
inside LI
isn't allowed, but I've seen it lately on many "big" websites like Smashing Magazine, Web Designer Wall... etc.
我知道DIV
里面LI
是不允许的,但我最近在许多“大”网站上看到了它,比如Smashing Magazine、Web Designer Wall等。
I tried to validate sites, and they have errors, but nothing about DIV
in LI
?!
我试图验证的网站,他们有错误,但只字未提DIV
中LI
?!
So can I use it inside LI
, and I need it to be valid?
那么我可以在里面使用它LI
吗,我需要它是有效的吗?
回答by Darin Dimitrov
Yes it is valid according to xhtml1-strict.dtd
. The following XHTML passes the validation:
是的,根据xhtml1-strict.dtd
. 以下 XHTML 通过验证:
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>
<body>
<ul>
<li><div>test</div></li>
</ul>
</body>
</html>
回答by Damon
As an addendum: Before HTML 5 while a div inside a li is valid, a div inside a dl, dd, or dt is not!
作为附录:在 HTML 5 之前,虽然 li 中的 div 有效,但 dl、dd 或 dt 中的 div 无效!
回答by Messa
If you look at xhtml1-strict.dtd, you'll see
如果你查看xhtml1-strict.dtd,你会看到
<!ELEMENT li %Flow;>
<!ENTITY % Flow "(#PCDATA | %block; | form | %inline; | %misc;)*">
<!ENTITY % block
"p | %heading; | div | %lists; | %blocktext; | fieldset | table">
Thus div
, p
etc. can be inside li
(according to XHTML 1.0 Strict DTD from w3.org).
因此div
,p
等可以在里面li
(根据 w3.org 的 XHTML 1.0 Strict DTD)。
回答by mystrdat
If I recall correctly, a div inside a li used tobe invalid.
如果我没记错的话, li 中的 div曾经是无效的。
@Flower @Superstringcheese Div should semantically define a section of a document, but it has already practically lost this role. Span should however contain text.
@Flower @Superstringcheese Div 应该在语义上定义文档的一部分,但它实际上已经失去了这个角色。但是,跨度应包含文本。
回答by mystrdat
I see you would want to do this if you wanted to make, say, the whole box of a menu item clickable. I used to insert an 'li' tag in 'a' tags to do this but this seems more valid.
我知道如果你想让菜单项的整个框都可点击,你会想要这样做。我曾经在“a”标签中插入一个“li”标签来做到这一点,但这似乎更有效。
回答by Pedro Visnardi
I'm starting in the webdesign universe and i used DIVs inside LIs with no problem with the semantics. I think that DIVs aren't allowed on lists, that means you can't put a DIV inside an UL, but it has no problem inserting it on a LI (because LI are just list items haha) The problem that i have been encountering is that sometimes the DIV behaves somewhat different from usual, but nothing that our good CSS can't handle haha. Anyway, sorry for my bad english and if my response wasn't helpful haha good luck!
我从网页设计领域开始,我在 LI 中使用了 DIV,语义没有问题。我认为列表中不允许 DIV,这意味着您不能将 DIV 放在 UL 中,但是将其插入 LI 没有问题(因为 LI 只是列表项哈哈)我遇到的问题是有时 DIV 的行为与平常有些不同,但没有什么是我们优秀的 CSS 无法处理的,哈哈。无论如何,对不起我的英语不好,如果我的回答没有帮助哈哈祝你好运!