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

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

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

**text to change** text t
14条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 08:15

    Update 2018

    Since this is a pretty popular answer I decided to update and beautify it a little by adding the textnode selector to jQuery as a plugin.

    In the snippet below you can see that I define a new jQuery function that gets all (and only) the textNodes. You can chain of this function as well with for example the first() function. I do a trim on the text node and check if it's not empty after the trim because spaces, tabs, new lines, etc. are also recognized as text nodes. If you need those nodes too then simple remove that from the if statement in the jQuery function.

    I added an example how to replace first text node and how to replace all text nodes.

    This approach makes it easier to read the code and easier to use it multiple times and with different purposes.

    The Update 2017 (adrach) should still work as well if you prefer that.

    As jQuery extension

    //Add a jQuery extension so it can be used on any jQuery object
    jQuery.fn.textNodes = function() {
      return this.contents().filter(function() {
        return (this.nodeType === Node.TEXT_NODE && this.nodeValue.trim() !== "");
      });
    }
    
    //Use the jQuery extension
    $(document).ready(function(){
      $('#replaceAll').on('click', () => {
        $('#testSubject').textNodes().replaceWith('Replaced');
      });
    
      $('#replaceFirst').on('click', () => {
        $('#testSubject').textNodes().first().replaceWith('Replaced First');
      });
    });
    p {
      margin: 0px;
    }
    
    
    **text to change**

    text that should not change

    text that should not change

    **also text to change**

    text that should not change

    text that should not change

    **last text to change**

    Javascript (ES) eqivilant

    //Add a new function to the HTMLElement object so it cna be used on any HTMLElement
    HTMLElement.prototype.textNodes = function() {
      return [...this.childNodes].filter((node) => {
        return (node.nodeType === Node.TEXT_NODE && node.nodeValue.trim() !== "");
      });
    }
    
    //Use the new HTMLElement function
    document.addEventListener('DOMContentLoaded', () => {
      document.querySelector('#replaceAll').addEventListener('click', () => {
        document.querySelector('#testSubject').textNodes().forEach((node) => {
          node.textContent = 'Replaced';
        });
      });
    
      document.querySelector('#replaceFirst').addEventListener('click', function() {
        document.querySelector('#testSubject').textNodes()[0].textContent = 'Replaced First';
      });
    });
    p {
      margin: 0px;
    }
    
    
    **text to change**

    text that should not change

    text that should not change

    **also text to change**

    text that should not change

    text that should not change

    **last text to change**


    Update 2017 (adrach):

    It looks like several things changed since this was posted. Here is an updated version

    $("div").contents().filter(function(){ return this.nodeType == 3; }).first().replaceWith("change text");
    

    Original answer (Not working for current versions)

    $("div").contents().filter(function(){ return this.nodeType == 3; })
    .filter(':first').text("change text");
    

    Source: http://api.jquery.com/contents/

提交回复
热议问题