首页 > 解决方案 > window.open 打开单个链接

问题描述

我正在制作一个学费管理程序,在该程序中我正在处理名为 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",
    "https://byjus.com/ncert-solutions-class-9-maths/"
  ],
  "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",
    "https://byjus.com/ncert-solutions-class-9-maths/"
  ],
  "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",
    "https://byjus.com/ncert-solutions-class-9-maths/"
  ],
  "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",
    "https://byjus.com/ncert-solutions-class-9-maths/"
  ],
  "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",
    "https://byjus.com/ncert-solutions-class-9-maths/"
  ],
  "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 个被打开。因此,根据这个站点,我将window.open(link, class_data);代码行替换为setTimeout(() => window.open(link), 3000);,但仍然只打开一个窗口。我对如何克服这个问题感到困惑。请帮帮我。

标签: javascriptjsonforeachwindow.open

解决方案


感谢@Lux 的帮助,该程序现在可以运行了。对于那些遇到同样问题的人,我建议他们点击谷歌窗口右上角的 3 个点,然后点击并settings输入Pop-ups and redirectssearch settings现在 google 将引导您到可以更改此设置的确切页面,在那里,只需更改Pop-ups and redirectsAllowed,现在您会发现 window.open 命令打开多个窗口。


推荐阅读