Get the string representation of a DOM node

前端 未结 11 2098
傲寒
傲寒 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:46

    Under FF you can use the XMLSerializer object to serialize XML into a string. IE gives you an xml property of a node. So you can do the following:

    function xml2string(node) {
       if (typeof(XMLSerializer) !== 'undefined') {
          var serializer = new XMLSerializer();
          return serializer.serializeToString(node);
       } else if (node.xml) {
          return node.xml;
       }
    }
    
    0 讨论(0)
  • 2020-12-01 04:46

    If your element has parent

    element.parentElement.innerHTML
    
    0 讨论(0)
  • 2020-12-01 04:47

    I dont think you need any complicated script for this. Just use

    get_string=(el)=>el.outerHTML;
    
    0 讨论(0)
  • 2020-12-01 04:47

    Try

    new XMLSerializer().serializeToString(element);
    
    0 讨论(0)
  • 2020-12-01 04:53

    if using react:

    const html = ReactDOM.findDOMNode(document.getElementsByTagName('html')[0]).outerHTML;
    
    0 讨论(0)
提交回复
热议问题