How to remove text inside an element with jQuery?

后端 未结 3 1252
鱼传尺愫
鱼传尺愫 2020-12-15 22:40

I have a script that creates a list of items with a structure like this:

  • Some stuff
  • 相关标签:
    3条回答
    • 2020-12-15 23:02
      $('li').html($(this).children())
      

      You can try this, it should work. Clean and easy.

      0 讨论(0)
    • 2020-12-15 23:07

      This should work.

      $("li").contents().filter(function(){ return this.nodeType != 1; }).remove();
      

      or by specifying text nodes explicitly

      $("li").contents().filter(function(){ return this.nodeType == 3; }).remove();
      

      See this fiddle.

      0 讨论(0)
    • 2020-12-15 23:16
      $('li').not('div,a').text('') 
      

      Try this, untested

      Edit

      $('li').contents().filter(function(){ return !(this.tagName == 'DIV' 
                                                  || this.tagName == 'A');}).remove();
      
      0 讨论(0)
    提交回复
    热议问题