JavaScript类似的功能不起作用

时间:2015-06-03 20:36:05

标签: javascript html

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

在我的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>

我的openBeep()

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

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

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


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

该脚本工作正常,如果我将所有onclicks替换为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);
}

1 个答案:

答案 0 :(得分:0)

启动Javascript程序员的快速提示:始终确保打开检查器/开发人员工具。在大多数浏览器中,您可以使用F12打开它。

然后在控制台中监控所有错误。喜欢你的问题(你自己解决了;-)你会看到一个错误,说找不到该功能。