Using .text() to retrieve only text not nested in child tags

后端 未结 25 2128
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 05:55

If I have html like this:

  • This is some text First span text
  • 25条回答
    •  误落风尘
      2020-11-21 06:36

      This seems like a case of overusing jquery to me. The following will grab the text ignoring the other nodes:

      document.getElementById("listItem").childNodes[0];
      

      You'll need to trim that but it gets you what you want in one, easy line.

      EDIT

      The above will get the text node. To get the actual text, use this:

      document.getElementById("listItem").childNodes[0].nodeValue;
      

    提交回复
    热议问题