如何解决使用参数调用函数并将其放置在获取请求中
input.addEventListener('keypress',function (e) {
if (e.key === 'Enter') {
getData();
}
});
function getData() {
let getSearchInput = document.getElementById("search-input").value;
fetch(`https://pokeapi.co/api/v2/pokemon/${getSearchInput}`)
.then((response) => response.json())
.then((data) => {
}
如何使用将动态替换“getSearchInput”变量的参数调用函数 getData?我希望能够在我的代码中的不同点调用具有不同 url 结尾的 getData 函数。感谢帮助!!
解决方法
您可以将值作为参数传递并根据条件检查该值是否存在
function getData(val) { // param
let getSearchInput = val ? val : document.getElementById("search-input").value;
fetch(`https://pokeapi.co/api/v2/pokemon/${getSearchInput}`)
.then((response) => response.json())
.then((data) => {
.....
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。