Javascript 无法设置属性';nodeValue';空的,需要解释

Javascript 无法设置属性';nodeValue';空的,需要解释,javascript,pug,Javascript,Pug,我知道有人问过这个问题,我也搜索过,但找不到类似的案例,所以我决定再次问这个问题 谁能告诉我这里的问题是什么,因为前两个轴X和Y工作得很好,但正是Z轴给了我 “无法将属性'nodeValue'设置为null” 我的网站出现错误。 我面临的确切问题是,前两个轴,X轴和Y轴确实毫无问题地传递了值,只有Z轴给了我上面的错误,我的问题是,为什么它给了我这个错误,为什么只有这一个,而不是全部3个。 需要注意的是,Axis-Z if语句中的msg.payload确实携带了一个其假定的值 下面是Javascr

我知道有人问过这个问题,我也搜索过,但找不到类似的案例,所以我决定再次问这个问题

谁能告诉我这里的问题是什么,因为前两个轴X和Y工作得很好,但正是Z轴给了我

“无法将属性'nodeValue'设置为null”

我的网站出现错误。
我面临的确切问题是,前两个轴,X轴和Y轴确实毫无问题地传递了值,只有Z轴给了我上面的错误,我的问题是,为什么它给了我这个错误,为什么只有这一个,而不是全部3个。 需要注意的是,Axis-Z if语句中的msg.payload确实携带了一个其假定的值

下面是Javascript:

 if (index.contains('Gyroscope')) { //Get all data from Gyroscope queue

    if((index.indexOf('Axis-X')) >= 0){//X-Axis queue  && 'X-Axis'
      // var sendData1 = msg.payload;
      // printText("gyrX", sendData1); //Publish data to the textArea
      // //console.log("I send Axis-X");
      document.getElementById('gyrX').firstChild.nodeValue = msg.payload;
    };

    if((index.indexOf('Axis-Y')) >= 0){
      // var sendData1 = msg.payload;
      // printText("gyrY", sendData1); //Publish data to the textArea
      // //console.log("I send Axis-Y");
      document.getElementById('gyrY').firstChild.nodeValue = msg.payload;
    };

    if((index.indexOf('Axis-Z')) >= 0){
      //var sendData1 = msg.payload;
      //printText("gyrZ", msg.payload); //Publish data to the textArea
      console.log("I send Axis-Z" + msg.payload);
      document.getElementById('gyrZ').firstChild.nodeValue = msg.payload;
    };
  };
这是玉:

  main.hoc.container.clear(styles='width:100%;')
    .sidebar.four_quarter.first(styles='width:30%;')
      table
        tr
          th(colspan='4')  Gyroscope Readings
        tr
          td(style='width:50%;') Gyroscope X:
          td(style='width:50%;')
            span#gyrX  
        tr
          td(style='width:50%;') Gyroscope Y:
          td(style='width:50%;')
            span#gyrY  
        tr
          td(style='width:50%;') Gyroscope Z:
          td(style='width:50%;')
            span#gyrZ
      p
        | Note: Sensor accuracy at current configuration can have a difference of 5 degree, calibration is needed.
      p
        | Note: Sensor takes the angle it was turned on at as 0 degree, make sure that everything is leveled before turning system on.
      br

在来到这里之前,我简化了代码,这就是为什么很多内容都被注释掉了。另外,有人能给我解释一下,为什么会发生这种情况吗?

您的Jade定义了许多没有内容的span元素

您的JavaScript尝试访问这些跨度中的第一个子节点(期望在那里找到文本节点),但失败了,因为没有内容,所以没有文本节点


一组跨距,而不是他们第一个孩子的
nodeValue

可能需要一些额外的信息;我看不到你的HTML的ID。你的元素中有一个没有
.firstChild
。document.getElementById('gyrZ')为null的唯一方法是因为DOM中没有ID为'gyrZ'的元素。可能重复的@Gomes-
文档。getElementById('gyrZ')
不是
null
document.getElementById'(‘gyrZ’).firstChild
是。但如果是这样的话,那么为什么网站上前两行有文本,而第三行没有?下面是链接:我希望如此works@B.Chmielewski-前两个包含空格,第三个不包含空格。好吧,现在我觉得自己很愚蠢。谢谢你指出这一点,我在过去的两个小时里对此非常生气。