amp-html - 单击 amp 网页时动态添加 html
问题描述
使用 AMP 时是否可以在点击时将 HTML 动态添加到网页?
类似的东西
<button on="tap:AMP.setState({foo: 'amp-bind'})">Say "Hello amp-
bind"</button>
<div [text]="foo">This is the placeholder to append the new
content</div>
而不是文本HTML?
解决方案
您需要的可以使用 amp-script 来实现。
https://github.com/ampproject/amphtml/blob/master/extensions/amp-script/amp-script.md
来自 github:
<!-- hello-world.html -->
<amp-script layout="container" src="https://example.com/hello-world.js">
<button id="hello">Insert Hello World!</button>
</amp-script>
// hello-world.js
const button = document.getElementById('hello');
button.addEventListener('click', () => {
const el = document.createElement('h1');
el.textContent = 'Hello World!';
// `document.body` is effectively the <amp-script> element.
document.body.appendChild(el);
});
推荐阅读
- android - 从 Android 到 Cassandra 的远程查询
- php - Wordpress PHP 调用 MySQL 程序挂起
- oop - Kotlin 中的 OVERRIDE_BY_INLINE
- java - JAXB marshal to string 在不同计算机上的工作方式不同
- spring - Spring Boot Actutor Web 指标 - 禁用(或分组)http_server_requests_seconds_sum
- corda - 在 Corda 中创建抽象流和具体流
- php - PHP:读取 html 文件并访问 JS 变量
- python - 如何创建逻辑复杂的自定义排序功能?
- reactjs - 从存储在状态中的 JSON 显示
- excel - Excel 索引匹配计数 - 尝试计算匹配 2 个条件的多个值