javascript document.getElementById("images").children[0] 到一个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4764494/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:48:21  来源:igfitidea点击:

document.getElementById("images").children[0] to a string

javascripthtmldom

提问by Atomix

The toString() method will output [object HTMLImageElement]. I want a string representation of the the image element '<img src="..." />'. outerHTML returns undefined in firefox.

toString() 方法将输出 [object HTMLImageElement]。我想要图像元素的字符串表示形式'<img src="..." />'。在 Firefox 中,outerHTML 返回 undefined。

How can I accomplish this?

我怎样才能做到这一点?

回答by lonesomeday

outerHTMLis not cross-browser.

outerHTML不是跨浏览器的。

The easiest way is to clone the element and add it to a parent element, then get the innerHTMLof that:

最简单的方法是克隆元素并将其添加到父元素,然后获取其中innerHTML的:

var outer = document.createElement('outer'),
    child = document.getElementById(“images”).children[0].cloneNode(true);

outer.appendChild(child);

var imgHtml = outer.innerHTML;