Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
755 views
in Technique[技术] by (71.8m points)

javascript getElementById and convert it to String

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

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

thanks a lot in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...