手风琴的 HTML5 详细信息元素

发布于 2024-11-18 09:54:44 字数 836 浏览 4 评论 0原文

细节元素在语义上是否适合标记手风琴?

示例:

<div class="accordion">

<details open>
<summary>Section 1</summary>
</details>

<details>
<summary>Section 2</summary>
</details>

<details>
<summary>Section 3</summary>
</details>

</div>

我问这个问题是因为规范对其用法不是很清楚。它只是指出详细信息元素是一个公开小部件。 我当然不想将该元素用于不该做的事情。

编辑

这样的东西在语义上会更合适吗?

<article role="tablist">

<header role="tab" aria-expanded>Section 1</header>
<div role="tabpanel">
</div>

<header role="tab">Section 2</header>
<div role="tabpanel">
</div>

<header role="tab">Section 3</header>
<div role="tabpanel">
</div>

</article>

谢谢!

Is the details element semantically appropriate to markup an accordion?

Example:

<div class="accordion">

<details open>
<summary>Section 1</summary>
</details>

<details>
<summary>Section 2</summary>
</details>

<details>
<summary>Section 3</summary>
</details>

</div>

I ask this question because the spec isn't very clear about its usage. It just states the details element is a disclosure widget.
I certainly don't want to use the element for something that it's not meant to be.

EDIT

Would something like this be better suited semantically?

<article role="tablist">

<header role="tab" aria-expanded>Section 1</header>
<div role="tabpanel">
</div>

<header role="tab">Section 2</header>
<div role="tabpanel">
</div>

<header role="tab">Section 3</header>
<div role="tabpanel">
</div>

</article>

Thanks!

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

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

发布评论

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

评论(3

听风吹 2024-11-25 09:54:44

是的,

元素完全适合您所描述的内容(和最语义化的选项):

来自http://www.w3.org/html/wg/drafts/html/master/interactive-elements.html#the-details-element

详细信息元素代表一个披露小部件,从中
用户可以获取附加信息或控件。 [强调我的]

这是来自 http://html5doctor.com/the-details -和摘要元素/

本质上,我们可以用来创建一个类似手风琴的小部件,用户可以打开和关闭它。在里面,我们可以放置任何我们想要的内容。

完美使用。例如,我的公司有一个包含许多联系选项的联系页面,因此我们可以为其设置一个手风琴,如下所示:

<details>
    <summary>Mailing Address</summary>
    <p><strong>U.S. Correspondence:</strong> 123 Main St., Washington, DC 00000-0000</p>
    <p><strong>International Correspondence:</strong> P.O.Box 1111, Washington, DC 00000-1111</p>
</details>
<details>
    <summary>Phone Numbers</summary>
    <p><strong>U.S.:</strong> 800-555-5555</p>
    <p><strong>Int'l:</strong> +1-800-555-5556</p>
</details>

关于样式的注释:良好的语义不应被丢弃来解决特定于浏览器的样式问题。 CSS Reset 可能是有序的,但是有没有语义上的理由不将这些有用且适当的元素用于手风琴。

**请注意:Chrome 目前是唯一支持切换功能的浏览器,我相信该规范旨在用于这些元素。 Javascript 后备在这里会很有用。

**编辑 3/2017 - 请参阅 http://caniuse.com/#feat=details 了解支持表。现在除了 IE 和 Edge 之外,几乎所有浏览器都支持此功能。

Yes, <details><summary> elements are entirely appropriate (and the most semantic option) for what you're describing:

From http://www.w3.org/html/wg/drafts/html/master/interactive-elements.html#the-details-element:

The details element represents a disclosure widget from which the
user can obtain additional information or controls. [emphasis mine]

And this is from http://html5doctor.com/the-details-and-summary-elements/:

Essentially, we can use to create an accordion-like widget that the user can toggle open and closed. Inside , we can put any sort of content we want.

Perfect usage. For example, my company has a contact page with many contact options so we could setup an accordion for it like so:

<details>
    <summary>Mailing Address</summary>
    <p><strong>U.S. Correspondence:</strong> 123 Main St., Washington, DC 00000-0000</p>
    <p><strong>International Correspondence:</strong> P.O.Box 1111, Washington, DC 00000-1111</p>
</details>
<details>
    <summary>Phone Numbers</summary>
    <p><strong>U.S.:</strong> 800-555-5555</p>
    <p><strong>Int'l:</strong> +1-800-555-5556</p>
</details>

A note on styling: good semantics shouldn't be thrown overboard to address browser-specific styling issues. A CSS Reset may be in order, but there's no semantic reason not to use these helpful and appropriate elements for an accordion.

**Do note: Chrome currently is the only browser that supports the toggling functionality that I believe the spec intended for these elements. A Javascript fallback would be useful here.

**Edit 3/2017 - See http://caniuse.com/#feat=details for support tables. This feature is now supported by almost all but IE and Edge.

黑寡妇 2024-11-25 09:54:44

不要为此使用

,否则您会遇到此问题:

疯狂的 Chrome 问题...Chrome 渲染扭曲?

这可能不是您的手风琴所需的行为。

Don't use <details> for this, or you'll have this problem:

Insane Chrome issue...Chrome renders twisties?

That's probably not desired behaviour for your accordion.

幽梦紫曦~ 2024-11-25 09:54:44

好问题。我正在寻找它,发现甚至 MDN 也在使用

作为其侧边栏链接菜单。 https://developer.mozilla.org/en-US/ docs/Web/HTML/Element/details

但是,有两个问题:

不可动画

由于它要么打开,要么不打开,所以没有办法为过渡设置动画 - 因为没有过渡。

来自 MDN 文档:

不幸的是,目前还没有内置的方法来设置动画
开放和封闭之间的过渡。

不支持 IE

如果您关心的话,Internet Explorer 不支持它:)

Nice question. I was searching for that and saw even MDN is using <details> for their sidebar menu of links. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/details

However, there are two problems:

<details> isn't animateable

Since it's either open or not, there's no way to animate the transition - because there's no transition.

From MDN docs:

Unfortunately, at this time there's no built-in way to animate the
transition between open and closed.

No support for IE

If you care, it is not supported by Internet explorer :)

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