【发布时间】:2021-05-13 15:19:55
【问题描述】:
我正在尝试使用 json 制作登录页面。我正在尝试拥有它,因此当有人单击它时,它会转到 json 文件中的页面。到目前为止,我有这个:
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200)
{
var Link = JSON.parse(this.responseText);
document.getElementById("Link1").innerHTML = Link.title;
}
};
xmlhttp.open("GET", "link.json", true);
xmlhttp.send();
function click1(){
window.location.href = Link.link;
}
当我点击它时,它会给我(从控制台):
(index):22 Uncaught ReferenceError: Link is not defined at click1 ((index):22) at HTMLAnchorElement.onclick ((index):8)
【问题讨论】:
-
Link是xmlhttp.onreadystatechange的本地,因此您无法从click1引用它。为什么不把整个 XMLHttpRequest 例程放在 click 函数体中呢?
标签: javascript html json