如何使它不会一次拉出这么多标签?

How to I make this not pull up so many tabs at once?

所以,我只想让网站在一行中提取 select 中的内容,这有效,但还会弹出 2 个额外的选项卡:(。有什么建议吗?我一直在努力工作在这一点上,我不想看到它继续:(

var options = [];
var urls = [];

function addOption(name, url) {


  var select = document.getElementById('optionlist');
  var option = document.createElement("OPTION");
  var optionText = document.createTextNode(name);

  option.appendChild(optionText);
  select.appendChild(option);

  if (options.length > 0) {
    options.push(name);
    urls.push(url)
  } else {
    options = [name];
    urls = [url];
  }


}


function createOptions() {
  addOption("List of Gemstones", "https://en.wikipedia.org/wiki/List_of_minerals");
  addOption("Another Website", "https://www.google.com");
}

document.getElementById('submit').onclick = function() {
  var select = document.getElementById('optionlist').value;

  for (a = 0; a <= options.length; a++) {
    var selected = options[a];

    if (selected == selected) {
      var toredirect = urls[a];
      var win = window.open(toredirect, '_blank');
      win.focus();
    } else if (selected === selected) {
      var toredirect = urls[a];
      var win = window.open(toredirect, '_blank');
      win.focus();
    }
  }
}

if (selected == selected) 始终为真,因为您只是将一个值与其自身进行比较(如果它包含 NaN 则例外,但这与此处无关)。所以循环的每次迭代都会执行window.open()代码。

我想你是想写 if (selected == select)。但是不需要循环查找选中的选项,它的索引在document.getElementById('optionlist').selectedIndex中。所以你可以简单地做:

document.getElementById('submit').onclick = function() {
    var index = document.getElementById('optionlist').selectedIndex;
    var toredirect = urls[index];
    var win = window.open(toredirect, '_blank');
    win.focus();
}

此外,在您的 addOption() 函数中,您不需要检查 options 的长度。在空数组上调用 push() 没有错。