CSS 我们可以在 <ul> 中与 <li> 一起使用任何其他 TAG 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2161337/
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
Can we use any other TAG inside <ul> along with <li>?
提问by Jitendra Vyas
Can we use any other TAG in <ul>
along with <li>
?
我们可以使用任何其他标记在<ul>
沿<li>
?
like
喜欢
<ul>
Some text here or <p>Some text here<p> etc
<li>item 1</li>
Some text here or <p>Some text here<p> etc
<li>item 1</li>
</ul>
回答by Jonny Haynes
For your code to be valid you can't put any tag inside a <ul>
other than an <li>
.
对于你的代码是有效的,你不能把任何标签内的<ul>
其他比<li>
。
You can however, put any block level element inside the <li>
, like so:
但是,您可以将任何块级元素放在 中<li>
,如下所示:
<ul>
<li>
<h2>...</h2>
<p>...</p>
<p>...</p>
</li>
</ul>
回答by ЯegDwight
According to the W3C Recommendation, the basic structure of a list must be:
根据W3C Recommendation,列表的基本结构必须是:
<UL>
<LI> ... first list item...
<LI> ... second list item...
...
</UL>
You can put p
tags only inside of li
tags, not as direct children of ul
. The W3C Recommendation expressly states:
您只能将p
标签放在标签内li
,而不是作为ul
. W3C 建议书明确指出:
lists are made up of sequences of list items defined by the LI element
列表由 LI 元素定义的列表项序列组成
回答by marcgg
While you could have other tags inside (and it might work just fine), you shouldn't because it's not w3c-compliant. It also makes very little semantic sense.
虽然你可以在里面有其他标签(它可能工作得很好),但你不应该因为它不符合 w3c。它也几乎没有语义意义。
Just create a simple page and run it throught the validator ( http://validator.w3.org/) and you'll see for yourself :)
只需创建一个简单的页面并通过验证器(http://validator.w3.org/)运行它,您就会亲眼看到:)
回答by Umair Hamid
You can use template tag and it is totally legal. For example:
您可以使用模板标签,这是完全合法的。例如:
<ul>
**<template>**Some text here or <p>Some text here<p> etc**</template>**
<li>item 1</li>
**<template>**Some text here or <p>Some text here<p> etc**</template>**
<li>item 1</li>
</ul>
回答by drdiv
No, this would be invalid markup. However, if you're trying to achieve a different look and feel for some of the lines, you can do this by adding a specific classname to any li tag instead. See below:
不,这将是无效标记。但是,如果您想为某些行实现不同的外观和感觉,您可以通过将特定的类名添加到任何 li 标签来实现。见下文:
<ul>
<li class="foobar">Label</li>
<li>Some text here</li>
<li>Some text here</li>
<li class="foobar">Label</li>
<li>Some text here</li>
<li>Some text here</li>
</ul>
回答by gingerbreadboy
You canwrite such mark up but you shouldn'tas it is non-compliant and you may get strange and unexpected results in different browsers.
您可以编写这样的标记,但您不应该这样做,因为它不合规,并且在不同的浏览器中可能会得到奇怪和意外的结果。
回答by pixeltocode
if your doing this to style a part of the list-element, you could do this
如果你这样做是为了设计列表元素的一部分,你可以这样做
<ul>
<li>normal text <span>bold text</span></li>
<li>normal text <span>bold text</span></li>
</ul>
and then use the CSS
然后使用 CSS
ul li {font-size: 12px; font-weight: normal;}
ul li span {font-size: 12px; font-weight: bold;}
hope this helps
希望这可以帮助
回答by jps
Knockout.js specific answer, you can use the following comment syntax.
Knockout.js 具体回答,可以使用下面的注释语法。
<ul>
<li class="header">Header item</li>
<!-- ko foreach: myItems -->
<li>Item <span data-bind="text: $data"></span></li>
<!-- /ko -->
</ul>
<script type="text/javascript">
ko.applyBindings({
myItems: [ 'A', 'B', 'C' ]
});
</script>
回答by Rahul_Daksh
we can't put any tag inside ul other than li. However, we can put other tags inside li, like this::
除了 li 之外,我们不能在 ul 中放置任何标签。但是,我们可以在 li 中放入其他标签,如下所示:
<ul>
<li>
<div>
<h2>Some Dummmy text</h2>
<p> some content </p>
---
---
</div>
<li/>
<li><li/>
<li><li/>
----
----
</ul>
回答by Y. Shoham
No. If it's list, it has list-items in it. I can't see any use for list with non-list-items in it; it's not list...
不。如果是列表,则其中包含列表项。我看不到列表中有非列表项的任何用途;这不是清单...