javascript getElementById 并将其转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2470089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript getElementById and convert it to String
提问by bombo
is there a way to convert a javascript HTML object to a string? i.e.
有没有办法将 javascript HTML 对象转换为字符串?IE
var someElement = document.getElementById("id");
var someElementToString = someElement.toString();
thanks a lot in advance
非常感谢提前
回答by Andy E
If you want a string representation of the entire tag then you can use outerHTMLfor browsers that support it:
如果你想要整个标签的字符串表示,那么你可以使用outerHTML支持它的浏览器:
var someElementToString = someElement.outerHTML;
For other browsers, apparently you can use XMLSerializer:
对于其他浏览器,显然您可以使用 XMLSerializer:
var someElement = document.getElementById("id");
var someElementToString;
if (someElement.outerHTML)
someElementToString = someElement.outerHTML;
else if (XMLSerializer)
someElementToString = new XMLSerializer().serializeToString(someElement);
回答by kennebec
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.
您始终可以将元素的克隆包装在“屏幕外”的空容器中。容器的innerHTML 是克隆和原始的'outerHTML'。将 true 作为第二个参数传递以获取元素的后代。
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;
}
回答by Darin Dimitrov
someElement.innerHTML
回答by Gregtheitroade
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
正如 Darin Dimitrov 所说,您可以使用 element.innerHTML 来显示 HTML 元素 childnodes HTML。如果您在 IE 下,则可以使用元素加上其后代节点 HTML 的外部 HTML 属性
回答by coolplays
str=getElementById('****').innerHTML
$i('result').innerHTML=">PLAINTEXT<"+str+"\>/PLAINTEXT<"
回答by YASH
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.
测试一下..它肯定会工作。

