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

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

If I have html like this:

  • This is some text First span text
  • 25条回答
    •  失恋的感觉
      2020-11-21 06:34

      Similar to the accepted answer, but without cloning:

      $("#foo").contents().not($("#foo").children()).text();
      

      And here is a jQuery plugin for this purpose:

      $.fn.immediateText = function() {
          return this.contents().not(this.children()).text();
      };
      

      Here is how to use this plugin:

      $("#foo").immediateText(); // get the text without children
      

    提交回复
    热议问题