问题描述
我正在尝试让Amp-AutoComplete直接转到一个URL,当单击该URL时,它将从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>
推荐答案
有趣的问题。
起初,我尝试使用css来解决这个问题,例如,为链接设置较高的z索引,但不起作用。
最后,我使用amp-AutoComplete中的on=";select";事件进行了选择。我希望这个选项适合您: 数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假"><!--
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>
代码:https://codepen.io/alexandr-kazakov/pen/rNeVoVo
我已经在手机和电脑上测试过了,运行良好。