如何重定向页面放大器? [英] How to redirect a page amp?

查看:69
本文介绍了如何重定向页面放大器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将以下javascript代码调整为适用于amp页面.

I need to adapt the following javascript code to an amp page.

<select id="url"> 
   <option value='home'>home</option> 
   <option value='contact'>contact</option> 
</select>

<input type="button" value='ir' onclick='ir()'> 

<script> 
function ir(){ 
   var url=document.getElementById("url").value; 
   location.href=url; 
}
</script>

有可能吗?

在文档中我找不到答案.

In the documentation I did not find the answer.

推荐答案

您正在使用按钮和onclick函数,在AMP中,您可以使用锚标记<a href="Value">Value</a>并使用`amp-bind'

You are using button and onclick function, in place of this in AMP you can use anchor tag <a href="Value">Value</a> and change the url using `amp-bind'

您可以使用amp-bind实现目标

> 这是有效的URL

在标头中添加JS

<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>

在正文中添加代码

<div class="wrapper">

    <select on="change:AMP.setState({ url: event.value })">
      <option value='home'>home</option> 
      <option value='contact'>contact</option> 
    </select>

    <a [href]="url" href="home">ir</a>

</div>

已更新-如果某人想更改选项时重定向而不是使用navigateTo操作

Updated - if some one want to redirect on option change than use navigateTo action

示例:

<select on="change:AMP.navigateTo(url=event.value)">
    <option value="http://google.com">google.com</option>
    <option value="http://yahoo.com">yahoo.com</option>
    <option value="http://bing.com">bing.com</option>
  </select>

这篇关于如何重定向页面放大器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆