兼容outHTML
css
div {
background: #0000FF;
width: 100px;
height: 100px;
}
span {
background: #00FF00;
width: 100px;
height: 100px;
}
p {
background: #FF0000;
width: 100px;
height: 100px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
html
<body>
<!--<div id="d1"></div>-->
<div id="a"><span>SPAN</span>DIV</div>
<span>SPAN</span>
<p>P</p>
</body>
1
2
3
4
5
6
2
3
4
5
6
js代码
function getOuterHTML(id) {
var el = document.getElementById(id);
var newNode = document.createElement("div");
document.body.appendChild(newNode);
var clone = el.cloneNode(true);
newNode.appendChild(clone);
document.body.removeChild(newNode);
return newNode.innerHTML;
}
window.onload = function () { getOuterHTML("a"); }
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
