【问题标题】:AMP: amp-accordion not workingAMP:amp-手风琴不工作
【发布时间】:2016-06-22 22:55:39
【问题描述】:

谁能提供amp-accordion 的工作示例?当我在引用

的 amp 页面中尝试来自 https://www.ampproject.org/docs/reference/extended/amp-accordion.html 的示例代码时
<script async custom-element="amp-accordion" src="https://cdn.ampproject.org/v0/amp-accordion-0.1.js"></script> 

在标题中,我收到以下两条(令人不安的)错误消息:

The attribute 'custom-element' in tag 'amp-access extension .js script' is set to the invalid value 'amp-accordion'. (see https://www.ampproject.org/docs/reference/extended/amp-access.html)

最后

The tag 'amp-accordion' is disallowed.

有什么想法吗?

【问题讨论】:

    标签: amp-html


    【解决方案1】:

    在 head 标签内添加amp JS 和amp accordion JS,如下所示

    <!--
      ## Introduction
    
      An accordion provides a way for viewers to have a glance at the outline of the content and jump to a section or their choice at their will.
    -->
    <!-- -->
    <!doctype html>
    <html ⚡>
    <head>
      <meta charset="utf-8">
      <title>amp-accordion</title>
      <script async src="https://cdn.ampproject.org/v0.js"></script>
      <!-- ## Setup -->
      <!--
        Import the `amp-accordion` component.
      -->
      <script async custom-element="amp-accordion" src="https://cdn.ampproject.org/v0/amp-accordion-0.1.js"></script>
      <link rel="canonical" href="https://ampbyexample.com/components/amp-accordion/">
      <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
      <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
      <style amp-custom>
      amp-accordion section[expanded] .show-more {
        display: none;
      }
      amp-accordion section:not([expanded]) .show-less {
        display: none;
      }
      .nested-accordion h4 {
        font-size: 14px;
        background-color: #ddd;
      }
      amp-accordion#hidden-header section[expanded] h4 {
        border: none;
      }
    </style>
    </head>
    <body>
    
      
        <amp-accordion disable-session-states>
          <section>
            <h4>Section 1</h4>
            <p>Bunch of content.</p>
          </section>
          <section>
            <h4>Section 2</h4>
            <amp-accordion class="nested-accordion">
              <section>
                <h4>Nested Section 2.1</h4>
                <p>Bunch of content.</p>
              </section>
              <section>
                <h4>Nested Section 2.2</h4>
                <p>Bunch of more content.</p>
              </section>
            </amp-accordion>
          </section>
        </amp-accordion>
    </body>
    </html>

    【讨论】:

      【解决方案2】:

      在此处查找有关如何使用 amp-accordion 的示例: https://ampbyexample.com/components/amp-accordion/

      【讨论】:

        【解决方案3】:

        虽然它是实验性的,但有必要启用它。请注意,仅 toggleExperiment 是不够的,您需要检查该实验是否尚未启用。请参见下面的示例。

        <script async custom-element="amp-accordion" src="https://cdn.ampproject.org/v0/amp-accordion-0.1.js"></script>
        <script>    
            (window.AMP = window.AMP || []).push(function(AMP) {
               if(!AMP.isExperimentOn('amp-accordion'))
                  AMP.toggleExperiment('amp-accordion');
            });
        </script>
        

        别忘了为它添加样式:

           <style amp-custom>
                section[expanded] span.expanded {
                    display: block;
                }
                section[expanded] span.collapsed {
                    display: none;
                }
                section:not([expanded]) span.expanded {
                    display: none;
                }
                section:not([expanded]) span.collapsed {
                    display: block;
                }
           </style>
        

        【讨论】:

          【解决方案4】:

          According to this page,它似乎是一个实验性组件,必须是 manually enabled here 并且在控制台中使用以下 javascript 行:

          AMP.toggleExperiment('amp-accordion')
          

          【讨论】:

          • 除了@Jupe 的回答,你会得到错误 The attribute 'custom-element' in tag 'amp-access extension .js script' is set to the invalid value 'amp-accordion'。 当属性 type 是除必需值之外的任何值时:application/json。当脚本标签的src 属性无效时也会抛出错误。
          • 谢谢,这就是我需要知道的一切......将评论它一段时间并等待这个组件会发生什么;)
          猜你喜欢
          • 2012-10-07
          • 2011-05-30
          • 1970-01-01
          • 2016-09-02
          • 2014-10-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-06
          相关资源
          最近更新 更多