Get the string representation of a DOM node

前端 未结 11 2097
傲寒
傲寒 2020-12-01 03:56

Javascript: I have the DOM representation of a node (element or document) and I\'m looking for the string representation of it. E.g.,

var el = document.creat         


        
相关标签:
11条回答
  • 2020-12-01 04:27

    I've found that for my use-cases I don't always want the entire outerHTML. Many nodes just have too many children to show.

    Here's a function (minimally tested in Chrome):

    /**
     * Stringifies a DOM node.
     * @param {Object} el - A DOM node.
     * @param {Number} truncate - How much to truncate innerHTML of element.
     * @returns {String} - A stringified node with attributes
     *                     retained.
     */
    function stringifyEl(el, truncate) {
        var truncateLen = truncate || 50;
        var outerHTML = el.outerHTML;
        var ret = outerHTML;
        ret = ret.substring(0, truncateLen);
    
        // If we've truncated, add an elipsis.
        if (outerHTML.length > truncateLen) {
          ret += "...";
        }
        return ret;
    }
    

    https://gist.github.com/kahunacohen/467f5cc259b5d4a85eb201518dcb15ec

    0 讨论(0)
  • 2020-12-01 04:33

    Use element.outerHTML to get full representation of element, including outer tags and attributes.

    0 讨论(0)
  • 2020-12-01 04:34

    You can create a temporary parent node, and get the innerHTML content of it:

    var el = document.createElement("p");
    el.appendChild(document.createTextNode("Test"));
    
    var tmp = document.createElement("div");
    tmp.appendChild(el);
    console.log(tmp.innerHTML); // <p>Test</p>
    

    EDIT: Please see answer below about outerHTML. el.outerHTML should be all that is needed.

    0 讨论(0)
  • 2020-12-01 04:34

    Use Element#outerHTML:

    var el = document.createElement("p");
    el.appendChild(document.createTextNode("Test"));
    
    console.log(el.outerHTML);
    

    It can also be used to write DOM elements. From Mozilla's documentation:

    The outerHTML attribute of the element DOM interface gets the serialized HTML fragment describing the element including its descendants. It can be set to replace the element with nodes parsed from the given string.

    https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML

    0 讨论(0)
  • 2020-12-01 04:40

    What you're looking for is 'outerHTML', but wee need a fallback coz it's not compatible with old browsers.

    var getString = (function() {
      var DIV = document.createElement("div");
    
      if ('outerHTML' in DIV)
        return function(node) {
          return node.outerHTML;
        };
    
      return function(node) {
        var div = DIV.cloneNode();
        div.appendChild(node.cloneNode(true));
        return div.innerHTML;
      };
    
    })();
    
    // getString(el) == "<p>Test</p>"
    

    You'll find my jQuery plugin here: Get selected element's outer HTML

    0 讨论(0)
  • 2020-12-01 04:45

    I have wasted a lot of time figuring out what is wrong when I iterate through DOMElements with the code in the accepted answer. This is what worked for me, otherwise every second element disappears from the document:

    _getGpxString: function(node) {
              clone = node.cloneNode(true);
              var tmp = document.createElement("div");
              tmp.appendChild(clone);
              return tmp.innerHTML;
            },
    
    0 讨论(0)
提交回复
热议问题