javascript getElementById and convert it to String

后端 未结 6 944
予麋鹿
予麋鹿 2020-12-11 05:49

is there a way to convert a javascript HTML object to a string? i.e.

var someElement = document.getElementById(\"id\");
var someElementToString = someElement         


        
6条回答
  •  情歌与酒
    2020-12-11 06:25

    You can always wrap a clone of an element in an 'offscreen', empty container. The container's innerHTML is the 'outerHTML' of the clone- and the original. Pass true as a second parameter to get the element's descendents as well.

    document.getHTML=function(who,deep){ 
     if(!who || !who.tagName) return '';
     var txt, el= document.createElement("div");
     el.appendChild(who.cloneNode(deep));
     txt= el.innerHTML;
     el= null;
     return txt;
    }
    

提交回复
热议问题