【问题标题】:Cannot read property 'addEventListener' of null (JavaScript Sails App)无法读取 null 的属性“addEventListener”(JavaScript Sails 应用程序)
【发布时间】:2016-11-25 02:09:45
【问题描述】:

我正在尝试在sails 应用程序中使用onsip 的以下JavaScript 代码来应用视频通话,但它给出了错误:

无法读取 null 的属性 addEventListener'

下面是它的 JavaScript 代码:

button.addEventListener('click', function() {
  // Was on a call, so the button press means we are hanging up
  if (onCall) {
    onCall = false;
    button.firstChild.nodeValue = 'video';
    remoteRender.style.visibility = 'hidden';
    session.bye();
    session = null;
  }
  // Was not on a call, so the button press means we are ringing someone
  else {
    onCall = true;
    button.firstChild.nodeValue = 'hang up';
    remoteRender.style.visibility = 'visible';
    session = makeCall(userAgent, target,
      false, true,
      remoteRender, null);
    session.on('bye', function() {
      onCall = false;
      button.firstChild.nodeValue = 'video';
      remoteRender.style.visibility = 'hidden';
      session = null;
    });
  }
});

上面的HTML代码是:

<button id="alice-video-button" class="right" type="button">video</button>

【问题讨论】:

    标签: javascript angularjs node.js sails.js


    【解决方案1】:

    据我所知,此错误:“无法读取 null 的属性 'addEventListener'”

    如果您在代码中的任何地方遇到此错误,则需要检查 id 和选择器。

    button.addEventListener('click', function () {..}
    

    您可能在这一行得到错误并检查此“按钮”变量是否已设置。

    希望对你有帮助

    【讨论】:

    • 谢谢你能看看上面的代码然后说我是哪里错了 dng
    • 您的代码似乎没有错,但正如我所说,只有这部分没有错。你可以写下你的函数var button = document.getElementById('alice-video-button'); 在这之后写你的代码button.addEventListener...
    猜你喜欢
    • 1970-01-01
    • 2014-11-24
    • 2021-12-17
    • 2021-07-27
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    相关资源
    最近更新 更多