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

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

If I have html like this:

  • This is some text First span text
  • 25条回答
    •  花落未央
      2020-11-21 06:46

      To be able to trim the result, use DotNetWala's like so:

      $("#foo")
          .clone()    //clone the element
          .children() //select all the children
          .remove()   //remove all the children
          .end()  //again go back to selected element
          .text()
          .trim();
      

      I found out that using the shorter version like document.getElementById("listItem").childNodes[0] won't work with jQuery's trim().

    提交回复
    热议问题