ayj*_*jay 4 html javascript dom
我目前正在尝试获取DOM元素(及其整个子元素)并将其存储为字符串,以便我可以在页面上将其重新显示为纯文本.与此类似:
// Get DOM Element
var x = document.getElementById("para1");
// Create a new DOM Element and display x as text on the page
var div = document.createElement('div');
div.textContent = x;
document.body.appendChild(div);
Run Code Online (Sandbox Code Playgroud)
我期待着我会得到类似的东西<ul><li>One</li><li>Two</li><li>Three</li></ul>; 然而,我得到的是[object HTMLUListElement].
使用x.outerHTML得到的HTML内容,包括元素本身
var div = document.createElement('div');
div.textContent = x.outerHTML; // x.innerHTML instead if you only want the contents
document.body.appendChild(div);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1852 次 |
| 最近记录: |