【问题标题】:Click on <details> (not <summary>) to close "disclosure statement"?点击<details>(不是<summary>)关闭“披露声明”?
【发布时间】:2021-03-13 01:25:44
【问题描述】:

按照此处显示的简单示例<details>: The Details disclosure element [Mozilla.org],如何单击“详细信息”以折叠这些详细信息?

例如,在动画 GIF 的上半部分下方是“摘要”,单击它会切换“详细信息”(如下)。

在某些情况下,这些细节会很长;我希望能够点击那里,折叠这些详细信息(而不是向上滚动到“摘要”部分)。

我正在通过 Ajax 生成我的 HTML 页面,因此我可能需要 Ajax/JQuery 解决方案。

这里是相关部分。

(function ($) {
    if (snippets_arr.length > 2) {
        fq_snippet += '<details><summary>' + snippets_arr.slice(0,2).join('<br/>');

        fq_snippet += '<br/>[toggle]</summary>' + snippets_arr.slice(2,).join('<br/>') + '</details>';
    }
    else {
        fq_snippet += snippets_arr.join('<br/>');
    }

  // Addendum: the solution involved this part of that file:
  init: function () {
      // add code here ...
        });
    });
  }
})(jQuery);


更新 1: 在与 @a.mola 聊天后解决了代码。单击“摘要”(顶部)切换打开/关闭“详细信息”视图(底部)。

新功能:点击“详细信息”(当然是打开时)会折叠“详细信息”视图。

这适用于页面上的所有(即多个)“详细信息/摘要”元素。


更新 2

我修改了代码以记住并在您折叠该部分后返回到滚动位置。

var scrollPosition = null;

/* REMEMBER, SET SCROLL POSITION
  *
  * Here I implemented a scroll position ["scrollPosition = $(window).scrollTop(); ..."] addition
  * to my "<details>" code, so that when you collapse a long <details> section you return to the 
  * original <summary> position.
  *
  *   https://stackoverflow.com/questions/17441065/how-to-detect-scroll-position-of-page-using-jquery
  *   https://stackoverflow.com/questions/10836428/how-to-maintain-page-scroll-position-after-a-jquery-event-is-carried-out/10836745
  *   https://stackoverflow.com/questions/32573532/jquery-how-to-return-to-the-exact-same-scroll-position-when-going-back-to-previ
  */

$(document).click(function(event) {
    /* ORIGINALLY (per StackOverflow discussion with user a.mola -- referenced above):
      *
      *   if (event.target.tagName == 'DETAILS' && event.target.hasAttribute('open')) event.target.removeAttribute('open');
      *
      * Modified to following, to allow use of "scrollPosition" to reset scroll position
      * when collapsing long <details> sections.
      */

    // if (event.target.tagName == 'DETAILS' && event.target.hasAttribute('closed')) {
    if (event.target.tagName == 'SUMMARY') {
        // SAVE THE CURRENT SCROLL POSITION:
        scrollPosition = $(window).scrollTop();
        // console.log('[summary click] scrollPosition', scrollPosition, '| type:', typeof scrollPosition)
        // ----------------------------------------
        /* NOTE: "return" here exits this AND following if statement: */
        // return scrollPosition;
    }   /* if (event.target.tagName == 'SUMMARY') {} */

    if (event.target.tagName == 'DETAILS' && event.target.hasAttribute('open')) {
        scrollPosition = scrollPosition;
        // ----------------------------------------
        /* COLLAPSE <details> ELEMENT BY CLICKING ON THAT [<details>] ELEMENT:
          *   Per user a.mola:
          *   https://stackoverflow.com/questions/66609261/click-on-details-not-summary-to-close-disclosure-statement/66609543#66609543
          */
        event.target.removeAttribute('open');
        // ----------------------------------------
        // SET THE SCROLL POSITION FROM STORED SESSION VALUE:
        $(window).scrollTop(scrollPosition);
    }  /* if (event.target.tagName == 'DETAILS' && event.target.hasAttribute('open')) {} */
});    /* $(document).click(function(event) {} */

【问题讨论】:

    标签: html toggle


    【解决方案1】:

    每当点击/触摸&lt;summary&gt;&lt;/summary&gt; 标记时,&lt;details&gt;&lt;/details&gt; 标记上的 open 属性就会设置以公开额外内容。

    只需切换&lt;details&gt;&lt;/details&gt; 标记上的属性即可实现您需要的功能。

    (function($) {
        // ...
        $(document).click(function(event) {
            if (event.target.tagName == 'DETAILS' && event.target.hasAttribute('open')) event.target.removeAttribute('open');
        });
    })(jQuery);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <details>
      <summary>Details</summary>
      Something small enough to escape casual notice.
    </details>

    【讨论】:

    • 嗨;感谢您的答复。我看到了几个类似的解决方案(StackOverflow ...),但我无法让它们工作。我认为这是因为我在 Ajax 生成的 HTML 页面中生成了该决策/摘要;但是,即使我使用您的代码制作了一个简单的 HTML 页面(我会将该代码附加到上面的问题中),它在 Firefox 86 或 Opera 74 中都不起作用。但是,这个 JSFiddle 确实可以工作(使用您的代码) . jsfiddle.net/10t26hmq ...这正是我想要的。
    • 那么,现在一切都好吗?如果是,请您将答案标记为正确吗?
    • 不,还没有;很抱歉造成混乱 - 请参阅我的 cmets:JSFiddle 和简单的 HTML 示例工作;我的 Ajax 代码不起作用。
    • 您用来生成此页面的代码,您知道如何准确获取您在 details 标签中的内容吗?我可以看到生成 HTML 的代码吗?
    • @VictoriaStuart 是的,妈妈,我现在已经编辑了我的代码......除非你想对 summary 标签做其他事情,否则不需要选择它
    猜你喜欢
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    相关资源
    最近更新 更多