How to change only text node in element

前端 未结 8 807
不知归路
不知归路 2020-11-28 10:40

I have next html:


And I want to chang

8条回答
  •  眼角桃花
    2020-11-28 10:51

    Evans solution added to jquery fn to make it's use comfortable:

    // get/change node content not children
    jQuery.fn.content = function( n ){
        var o = $(this).clone();
        var c = o.children().remove();
        if (typeof n === "string" ){
            o.html(n);
            $(this).html(c).append(n);
        }
        return o.html();
    }
    

    Usage :$('myselector').content('NewContentString');

提交回复
热议问题