Why does `childNodes` return a number larger than I expect?

前端 未结 6 2148
有刺的猬
有刺的猬 2020-12-01 17:02

Could you please look at this jsFiddle example, and tell me why the number \'11\' is alerted rather than \'5\' (the number of

  • elements)?

    From

  • 6条回答
    •  栀梦
      栀梦 (楼主)
      2020-12-01 17:43

      Text nodes are included in the child nodes count. To get the proper value, you'd need to strip out text nodes, or make sure they are not in your code. Any white space between code is considered a space and a text node, so your count is the total number of text nodes.

    提交回复
    热议问题