【问题标题】:AMP amp-autocomplete - go to url when clickedAMP amp-autocomplete - 单击时转到 url
【发布时间】:2020-08-10 21:17:21
【问题描述】:

我试图让 amp-autocomplete 直接转到单击时从 json 列表中获取的 url。 href 包含在结果中,但是当单击结果时,它只是将其添加到输入框中,而不是直接。

我试过on="tap:AMP.navigateTo(url='{{url}}')" 似乎没有做任何事情。不确定 amp 是否可以做到这一点?

<form class="sample-form" method="post" target="_top" action-xhr="https://amp.dev/documentation/examples/api/echo">
  <label>
    <span>Search for</span>
    <amp-autocomplete filter="token-prefix" filter-value="h1"  min-characters="2">
        <input type="search" name="h1">
      <script type="application/json">
            {
                "items":[
                        {
                            "h1":"page1",
                            "url":"page-1-url"
                        },{
                            "h1":"page2",
                            "url":"page-2-url"
                        },
                        {
                            "h1":"page3",
                            "url":"page-3-url"
                        }
                ]
            }
      </script>
            <template type="amp-mustache" id="amp-template-custom">
                <div class="slug-item" data-value="{{h1}}">
                        <a href="{{url}}" on="tap:AMP.navigateTo(url='{{url}}')">{{h1}}</a>   
                </div>
            </template>
    </amp-autocomplete>
  </label>
</form>

【问题讨论】:

    标签: autocomplete amp-html


    【解决方案1】:

    有趣的问题。

    一开始,我尝试使用 CSS 解决问题,例如为链接设置高 z-index,但没有奏效。

    最后,我选择了 amp-autocomplete 中的 on="select" 事件。我希望这个选项适合你:

    <!--
         This is the minimum valid AMP HTML document. Type away
         here and the AMP Validator will re-check your document on the fly.
    -->
    <!DOCTYPE html>
    <html ⚡>
      <head>
        <meta charset="utf-8" />
        <link rel="canonical" href="self.html" />
        <meta name="viewport" content="width=device-width,minimum-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
        >
        <script async src="https://cdn.ampproject.org/v0.js"></script>
        <script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
        <script async custom-element="amp-autocomplete" src="https://cdn.ampproject.org/v0/amp-autocomplete-0.1.js"></script>
        <script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
      </head>
      <body>
        <form class="sample-form" method="post" target="_top" action-xhr="https://amp.dev/documentation/examples/api/echo">
          <label>
            <span>Search for</span>
            <amp-autocomplete filter="token-prefix" filter-value="h1" min-characters="2" on="select:AMP.navigateTo(url=event.value)">
              <input type="search" name="h1" />
              <script type="application/json">
                {
                  "items": [
                    {
                      "h1": "page1",
                      "url": "https://stackoverflow.com/"
                    },
                    {
                      "h1": "page2",
                      "url": "https://google.com/"
                    },
                    {
                      "h1": "page3",
                      "url": "https://amp.dev/"
                    }
                  ]
                }
              </script>
              <template type="amp-mustache" id="amp-template-custom">
                <div class="slug-item" data-value="{{url}}">
                  <span class="autocomplete-link">{{h1}}</span>
                </div>
              </template>
            </amp-autocomplete>
          </label>
        </form>
      </body>
    </html>

    Codepen: https://codepen.io/alexandr-kazakov/pen/rNeVoVo

    我已经在手机和电脑上测试过,效果很好。

    【讨论】:

    • 如何在选择的输入字段中显示h1 值而不是url 值?
    猜你喜欢
    • 2017-12-26
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多