简体   繁体   English

获取整个innerHTML(父+子)

[英]Get the whole innerHTML (parent + child)

On a js script, I have a div which I add dynamically some childs. 在一个js脚本上,我有一个div,我动态添加了一些孩子。 So from the beginning, I can't know how many childs my div will contains. 所以从一开始,我就不知道我的div会包含多少个孩子。

When I try to get the innerHTML of my div container, I only obtains the html of the div. 当我尝试获取div容器的innerHTML时,我只获取div的html。 Not the html of the node. 不是节点的html。

How can I get the innerHTML of the whole components, through the childs ? 如何通过孩子获得整个组件的innerHTML?

Try this: 尝试这个:

document.getElementById('MyId').outerHTML

This gets all the html of the element, inside and itself. 这将获取元素的所有html,内部和自身。

在此输入图像描述


.outerHTML for outer, .innerHTML for inner. 外部的.outerHTML ,内部的.innerHTML

使用outerHTML就可以了。

document.getElementById('YourId').outerHTML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM