Remove tag around a text node using javascript

前端 未结 8 1938
夕颜
夕颜 2021-02-06 14:12

If I have some HTML that looks like this:

This is some text that is being written with a high
相关标签:
8条回答
  • 2021-02-06 14:53

    text.replace(/</?[^>]+(>|$)/g, "");

    0 讨论(0)
  • 2021-02-06 14:58
    element = document.getElementById("span id");
    element.parentNode.insertBefore(element.firstChild, element);
    element.parentNode.removeChild(element);
    
    0 讨论(0)
  • 2021-02-06 15:02

    This works:

    wrap = $('.highlight');
    wrap.before(wrap.text());
    wrap.remove();
    
    0 讨论(0)
  • 2021-02-06 15:04

    A better unwrap plugin:

    $.fn.unwrap = function() {
      this.parent(':not(body)')
        .each(function(){
          $(this).replaceWith( this.childNodes );
        });
    
      return this;
    };
    

    from Ben Alman

    0 讨论(0)
  • 2021-02-06 15:05

    You get the text, and replace the span with it:

    var wrap = $('.highlight');
    var text = wrap.text();
    wrap.replaceWith(text);
    
    0 讨论(0)
  • 2021-02-06 15:05

    wrap it in a plugin

    (function($) {   
        $.fn.tagRemover = function() {           
            return this.each(function() {            
            var $this = $(this);
            var text = $this.text();
            $this.replaceWith(text);            
            });            
        }    
    })(jQuery);
    

    and then use like so

    $('div span').tagRemover();
    

    Working Demo here - add /edit to the URL to play with the code

    0 讨论(0)
提交回复
热议问题