How to remove only tag using jQuery?

前端 未结 5 1730
心在旅途
心在旅途 2021-02-06 00:23

I want to remove the span using jQuery, I have tried the .unwrap(); but it\'s not working.


      
5条回答
  •  既然无缘
    2021-02-06 00:43

    Obviously, unwrap doesn't work as the spans only have text nodes inside them and jquery doesn't handle text nodes too well... this works however (you could use also jQuery.text instead of jQuery.html if you're sure that the span only contains text):

    $('li a span').replaceWith($('li a span').html());
    

    Working example

    Edit: Actually, it seems that unwrap works as well if you use jQuery.contents to work around the jquery's inability to directly select text nodes:

    $('li a span').contents().unwrap();
    

提交回复
热议问题