javascript - 当窗口宽度 => 768px 时打开 <details> 元素

标签 javascript html html-tag-details html-tag-summary


我正在开发一个响应式网站,我在其中使用 <details> <summary>在服务部分显示附加信息的元素。

当窗口宽度 > 768px 时,如何更改详细信息的“打开”属性?
只用 css 可以吗?


这是 html 代码:

<section id="services">
    <section>
        <details>
            <summary>info</summary>
            <p>A paragraph with especific information</p>
        </details>
   </section>
</section>  

我可以使用这样的 javascript 代码,但更喜欢 css:

  var desplegable = $("#services section details");
if ($(window).width() > 768) {
    if (desplegable.attr("open") != "open"){ 
        desplegable.attr('open','true');

    }
}

有什么想法吗?谢谢

特种部队: http://www.whatwg.org/specs/web-apps/current-work/multipage/interactive-elements.html#the-details-element

最佳答案

到目前为止,还没有办法修改 <details> 的显示。通过 CSS 的内容(例如:details > * { display: block; } 用于媒体查询。),因为似乎没有针对它的规范。

作为Amelia Bellamy-Royds puts it :

You can’t actually describe it’s basic behavior with the CSS model. The part that shows/hides isn’t in any particular element. Which means there’s no way to implement a media-query override that says if the screen is big enough, show this all the time. So it doesn’t replace all your show/hide navigation widgets.
Because open/closed is done through a simple attribute, and there is no pseudoclass, if you want to (a) have some elements open to start, and (b) change styles for open and closed, you might need some scripted support testing so your details[open] selectors don’t match on unsupporting browsers.

我想这就是问题所在。

您可以通过应用丑陋的 checkbox-hack 来处理此行为(完全基于 CSS 的隐藏/显示仅在某些媒体查询上切换)不过。

关于javascript - 当窗口宽度 => 768px 时打开 <details> 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15589248/

相关文章:

html - 根据 rails 4 中的页面显示不同的样式表

html - 详细信息和摘要标签兼容性问题

javascript - 暂停移相器动画

javascript - 预加载图像然后将其用作 div 中的背景

javascript - 如果 vue 中的特定选项卡没有显示任何内容,如何隐藏或不显示选项卡?

javascript - 如何检查值是否为空?

javascript - 外部 CSS 文件未加载到 HTML 文件中

javascript - Bootstrap/Javascript/JQuery 使按钮一次改变一种颜色