javascript getElementById and convert it to String

后端 未结 6 919
予麋鹿
予麋鹿 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:11

    If you want a string representation of the entire tag then you can use outerHTML for browsers that support it:

    var someElementToString = someElement.outerHTML;
    

    For other browsers, apparently you can use XMLSerializer:

    var someElement = document.getElementById("id");
    var someElementToString;
    
    if (someElement.outerHTML)
        someElementToString = someElement.outerHTML;
    else if (XMLSerializer)
        someElementToString = new XMLSerializer().serializeToString(someElement); 
    
    0 讨论(0)
  • 2020-12-11 06:18
    str=getElementById('****').innerHTML
    $i('result').innerHTML=">PLAINTEXT<"+str+"\>/PLAINTEXT<"
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-11 06:27

    As Darin Dimitrov said you can use element.innerHTML to display the HTML element childnodes HTML. If you are under IE you can use the outerHTML propoerty that is the element plus its descendants nodes HTML

    0 讨论(0)
  • 2020-12-11 06:27

    You just have to create one variable then store value into it. As in one my project I have done the same thing and it works perfectly.

    var message = ""; 
    message = document.getElementById('messageId').value;
    

    test it.. It will definitely work.

    0 讨论(0)
  • 2020-12-11 06:29
    someElement.innerHTML
    
    0 讨论(0)
提交回复
热议问题