无法读取属性'addEventListener'为null(JavaScript帆船应用程序)

Cannot read property 'addEventListener' of null (JavaScript Sails App)

本文关键字:JavaScript 应用程序 addEventListener 读取 属性 null      更新时间:2023-09-26

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

无法读取空的属性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>

据我所知,这个错误是:"无法读取null的属性'addEventListener'"

如果您在代码中的任何位置出现此错误,则需要检查id和选择器。

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

您可能会在这一行中得到错误,并检查是否设置了此"按钮"变量。

希望这将有助于