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

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

If I have html like this:

  • This is some text First span text
  • 25条回答
    •  死守一世寂寞
      2020-11-21 06:51

      It'll need to be something tailored to the needs, which are dependent on the structure you're presented with. For the example you've provided, this works:

      $(document).ready(function(){
           var $tmp = $('#listItem').children().remove();
           $('#listItem').text('').append($tmp);
      });
      

      Demo: http://jquery.nodnod.net/cases/2385/run

      But it's fairly dependent on the markup being similar to what you posted.

    提交回复
    热议问题