JQuery change inner text but preserve html

前端 未结 14 2754
名媛妹妹
名媛妹妹 2020-12-01 06:56

I would like to change the text of a HTML element but preserve the rest of the inner html with jQuery.

For instance:

Some         


        
14条回答
  •  再見小時候
    2020-12-01 07:47

    An efficient and robust way that doesn't require you to know what the inner elements are or what the text is:

    var $a = $('a');
    var inner = '';
    $a.children.html().each(function() {
        inner = inner + this.outerHTML;
    });
    $a.html('New text' + inner);
    

提交回复
热议问题