我想通过点击按钮来获取数据。
当前我的工作如下代码所示。
有什么办法把这个绑起来吗?
谢谢
var apikey="https://opentdb.com/api.php?amount=1&type=multiple";
fetch(apikey)
.then(response => response.json())
.then(json => {
console.log(json);
});<button type="button">get data</button>
发布于 2020-05-02 14:37:41
可以在使用代码的按钮的click上添加事件侦听器:
document.getElementById('btn').addEventListener('click', function(){
var apikey="https://opentdb.com/api.php?amount=1&type=multiple";
fetch(apikey)
.then(response => response.json())
.then(json => {
console.log(json);
});
})<button id="btn" type="button">get data</button>
发布于 2020-05-02 14:39:01
绑定单击函数。
async function fetchData(){
var apikey="https://opentdb.com/api.php?amount=1&type=multiple";
let data = await fetch(apikey)
.then(response => response.json());
console.log(data);
}<button type="button" onclick="fetchData()">get data</button>
https://stackoverflow.com/questions/61561045
复制相似问题