How can I change an element's text without changing its child elements?

前端 未结 14 2341
难免孤独
难免孤独 2020-11-22 07:30

I\'d like to update element\'s text dynamically:

**text to change** text t
14条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 08:21

    This is an old question but you can make a simple function like this to make your life easier:

    $.fn.toText = function(str) {
        var cache = this.children();
        this.text(str).append(cache);
    }
    

    Example:

    **text to change**

    text that should not change

    text that should not change

    Usage:

    $("#my-div").toText("helloworld");
    

提交回复
热议问题