问题描述
我正在制作一个学费管理程序,其中我正在处理名为 data.json 的给定 JSON 文件:
{
"9 Standard": [
"https://byjus.com/ncert-solutions-class-9-maths/","https://www.google.com/search?q=google+fonts&rlz=1C1CHBD_enIN950IN950&oq=google&aqs=chrome.0.69i59j69i57j69i60j69i65l2j69i60l3.2127j0j7&sourceid=chrome&ie=UTF-8"
],"9 Applied": [
"https://www.google.com/search?q=google+fonts&rlz=1C1CHBD_enIN950IN950&oq=google&aqs=chrome.0.69i59j69i57j69i60j69i65l2j69i60l3.2127j0j7&sourceid=chrome&ie=UTF-8","https://byjus.com/ncert-solutions-class-9-maths/"
],"10 Standard": [
"https://www.google.com/search?q=google+fonts&rlz=1C1CHBD_enIN950IN950&oq=google&aqs=chrome.0.69i59j69i57j69i60j69i65l2j69i60l3.2127j0j7&sourceid=chrome&ie=UTF-8","10 Applied": [
"https://www.google.com/search?q=google+fonts&rlz=1C1CHBD_enIN950IN950&oq=google&aqs=chrome.0.69i59j69i57j69i60j69i65l2j69i60l3.2127j0j7&sourceid=chrome&ie=UTF-8","11 Standard": [
"https://www.google.com/search?q=google+fonts&rlz=1C1CHBD_enIN950IN950&oq=google&aqs=chrome.0.69i59j69i57j69i60j69i65l2j69i60l3.2127j0j7&sourceid=chrome&ie=UTF-8","11 Applied": [
"https://www.google.com/search?q=google+fonts&rlz=1C1CHBD_enIN950IN950&oq=google&aqs=chrome.0.69i59j69i57j69i60j69i65l2j69i60l3.2127j0j7&sourceid=chrome&ie=UTF-8","12 Standard": [
"https://www.google.com/search?q=google+fonts&rlz=1C1CHBD_enIN950IN950&oq=google&aqs=chrome.0.69i59j69i57j69i60j69i65l2j69i60l3.2127j0j7&sourceid=chrome&ie=UTF-8","12 Applied": [
"https://www.google.com/search?q=google+fonts&rlz=1C1CHBD_enIN950IN950&oq=google&aqs=chrome.0.69i59j69i57j69i60j69i65l2j69i60l3.2127j0j7&sourceid=chrome&ie=UTF-8","https://byjus.com/ncert-solutions-class-9-maths/"
]
}
现在上面的 JSON 文件包含教师想要立即打开以开始上课的链接。所以我试着做如下安排:
我想做的程序截图
我编写的用于在计算机处理 data.json 数据时创建按钮的代码
<script>
fetch("data.json")
.then((response) => response.json())
.then(function (classes) {
console.log(classes);
Object.keys(classes).forEach(function func(class_data) {
let btn = document.createElement("button");
btn.innerHTML = class_data;
btn.name = class_data;
document.body.appendChild(btn);
btn.className = "button";
btn.onclick = function () {
links = classes[class_data];
links.forEach(func);
console.log(links);
function func(link) {
console.log(link);
window.open(link,class_data);
}
};
});
});
</script>
我希望每个按钮都能打开 2 个窗口,但只打开了 1 个。因此,根据 this site,我将代码的 window.open(link,class_data);
行替换为 setTimeout(() => window.open(link),3000);
,但仍然只打开了一个窗口。我对如何克服这个问题感到困惑。请帮帮我。
解决方法
感谢@Lux 的帮助,程序现在可以运行了。对于那些遇到同样问题的人,我建议他们点击谷歌窗口右上角的 3 个点,然后点击 settings
并在 {{1} 中输入 Pop-ups and redirects
} .现在 google 会引导您到可以更改此设置的确切页面,在那里,只需将 search settings
更改为 Pop-ups and redirects
,现在您会发现 window.open 命令打开多个窗口。