Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript类似的函数不起作用_Javascript_Html - Fatal编程技术网

JavaScript类似的函数不起作用

JavaScript类似的函数不起作用,javascript,html,Javascript,Html,我需要做3个按钮,每个按钮打开类似的窗口 在my index.html中,它如下所示 <li><button type="button" onclick="openBeep()">Beep</button></li> <li><button type="button" onclick="openFlash()">Flash</button></li> <li><button type

我需要做3个按钮,每个按钮打开类似的窗口

在my index.html中,它如下所示

<li><button type="button" onclick="openBeep()">Beep</button></li>
<li><button type="button" onclick="openFlash()">Flash</button></li>
<li><button type="button" onclick="window.open()">Indexing</button></li>
该脚本运行良好,如果我将所有onclick替换为openBeep,它们都会打开新窗口。。但是openFlash尽管本质上是相同的,但却什么也没做

function openFlash() {
  var x = window.open(''),
    button = x.document.createElement('button');
  button.innerHTML = 'Start Test';

  button.addEventListener('click', function () {
    x.alert('!');
  });

  var container = x.document.createElement('div');
  container.id = 'buttonParent';


  x.document.body.appendChild(container);
  container.appendChild(button);
}

开始Javascript程序员的快速提示:始终确保打开inspector/developer工具。在大多数浏览器中,都是用F12打开的


然后在控制台中监视所有错误。就像您的问题一样(您自己解决了;-),您会看到一个错误,表示找不到函数。

您正在为
窗口分配一个变量。打开
,然后对该变量调用
.alert
。。行得通吗?我猜无论你有什么
工作
,都不能正常工作,因为两个打开的窗口的名称是相同的。这意味着,如果您先单击openFlash,它将
工作
,但随后openBeep将不工作。@Rooster我刚刚在openFlash中更改了名称,但它不工作。此外,如果我在两个不同的按钮上调用openFlash来打开beep,它仍然会打开一个全新的窗口。。。我也试着按照你的建议先按openFlash,但它仍然不起作用。@tymeJV是的,就像调用
window.alert
,因为
window.open也是一个window对象(但弹出窗口的)@JuanMendes--啊,很高兴知道!
function openFlash() {
  var x = window.open(''),
    button = x.document.createElement('button');
  button.innerHTML = 'Start Test';

  button.addEventListener('click', function () {
    x.alert('!');
  });

  var container = x.document.createElement('div');
  container.id = 'buttonParent';


  x.document.body.appendChild(container);
  container.appendChild(button);
}