Remove tag around a text node using javascript

前端 未结 8 1976
夕颜
夕颜 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 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

提交回复
热议问题