javascript - Group `dt` and `dd` in `span` under `dl` - Stack Overflow

I'm trying to show or hide certain dtdd groups within a dl. Is this valid HTML? Should browsers p

I'm trying to show or hide certain dt/dd groups within a dl. Is this valid HTML? Should browsers plain about this or is it legit? I've looked at the dl specs but couldn't find anything saying that the dl can contain something other than dt and dd children. I need to do this as I'm using angularJS which provides this neat way of adding or removing elements from the DOM

<dl>
  <dt>Term</dt>
  <dd>Definition</dd>

  <span ng-if="true">
    <dt>Term 2</dt>
    <dd>Definition 2</dd>
  </span>
</dl>

I'm trying to show or hide certain dt/dd groups within a dl. Is this valid HTML? Should browsers plain about this or is it legit? I've looked at the dl specs but couldn't find anything saying that the dl can contain something other than dt and dd children. I need to do this as I'm using angularJS which provides this neat way of adding or removing elements from the DOM

<dl>
  <dt>Term</dt>
  <dd>Definition</dd>

  <span ng-if="true">
    <dt>Term 2</dt>
    <dd>Definition 2</dd>
  </span>
</dl>
Share Improve this question asked Jul 9, 2014 at 14:06 OktavOktav 2,1612 gold badges22 silver badges33 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 4

Grouping with div in dl is now valid per WHATWG HTML, see https://html.spec.whatwg/multipage/semantics.html#the-dl-element

The dl element

Content model:

Either: Zero or more groups each consisting of one or more dt elements followed by one or more dd elements, optionally intermixed with script-supporting elements.

Or: One or more div elements, optionally intermixed with script-supporting elements.

<dl>
 <div>
  <dt> Last modified time </dt>
  <dd> 2004-12-23T23:33Z </dd>
 </div>
 <div>
  <dt> Remended update interval </dt>
  <dd> 60s </dd>
 </div>
 <div>
  <dt> Authors </dt>
  <dt> Editors </dt>
  <dd> Robert Rothman </dd>
  <dd> Daniel Jackson </dd>
 </div>
</dl>

This is not valid HTML5, because the content model of dl elements is

Zero or more groups each consisting of one or more dt elements followed by one or more dd elements, optionally intermixed with script-supporting elements.

So dl can't contain span, but most browsers will render it correctly.

Instead of span, I suggest using di elements. They are invalid HTML5 too, but were valid in XHTML2.

<dl>
  <dt>Term</dt>
  <dd>Definition</dd>
  <di ng-if="true">
    <dt>Term 2</dt>
    <dd>Definition 2</dd>
  </di>
</dl>

However, the downside is that old IE won't recognize di, so it won't apply CSS styles to them unless you use document.createElement('di') while the document is loading.

That will probably work in most browsers but it is not valid HTML. It could also break some CSS. You probably need to just use 2 ng-if directives

<dt ng-if="true">Term 2</dt>
<dd ng-if="true">Definition 2</dd>

Note that if you need to do something like iterate over a list of items you can use angular's ng-repeat-start and ng-repeat-end.

<dt ng-repeat-start="item in list">Term 2</dt>
<dd ng-repeat-end>Definition 2</dd>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745191084a4615866.html

相关推荐

  • javascript - Group `dt` and `dd` in `span` under `dl` - Stack Overflow

    I'm trying to show or hide certain dtdd groups within a dl. Is this valid HTML? Should browsers p

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信