简体   繁体   English

如何将JavaScript DOM元素(及其整个结构)转换为字符串(不使用jQuery)? ..并在页面上重新显示为文本?

[英]How do I convert a JavaScript DOM element (and its entire structure) into string (without jQuery)? ..and re-display it on the page as text?

I'm currently trying to grab a DOM Element (and its entire sub-elements) and store it as string, so that I could re-display it as plain text on the page. 我目前正在尝试获取DOM元素(及其整个子元素)并将其存储为字符串,以便我可以在页面上将其重新显示为纯文本。 Similar to this below: 与此类似:

// 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); 

I was expecting I would get something like <ul><li>One</li><li>Two</li><li>Three</li></ul> ; 我原以为我会得到像<ul><li>One</li><li>Two</li><li>Three</li></ul> ; however, what I got instead was [object HTMLUListElement] . 但是,我得到的是[object HTMLUListElement]

Here's the fiddle: http://output.jsbin.com/wesosu/1 这是小提琴: http//output.jsbin.com/wesosu/1

Use x.outerHTML to get the HTML content including the element itself 使用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); 

暂无
暂无

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

相关问题 <a>使用jQuery</a>将<a>文本字符串</a>转换<a>为dom元素</a> - convert <a> text string to dom element with jQuery 如何将现有的DOM元素文本转换为HTML元素? - How do I convert existing DOM element text to HTML element? 我将如何使用jquery重新显示隐藏的div,该div将错误消息保持在新位置,并且错误消息一次显示为1? - How would I use jquery to re-display a hidden div that holds error messages in a new position with error messages shown 1 at a time? 使用JavaScript或JQuery,如何确定将DOM元素添加到页面的时间? - Using JavaScript or JQuery how do I determine when a DOM element is added to the page? 如何将字符串转换为 html 并插入到某个 dom 元素中? - How do I convert string to html and insert into certain dom element? 如何在没有id的dom元素上使用javascript选择并单击? - How do I select and click with javascript on dom element without id? 如何使用Selenium WebDriver转储整个dom结构 - How do I dump entire dom structure using selenium webdriver JavaScript / Jquery HTML将DOM结构/页面导出为HTML文件或文本 - JavaScript / Jquery HTML Export DOM Structure/Page to HTML File or Text 如何在JS中操作输入值以在我的HTML中重新显示 - how can I manipulate input value in JS to re-display in my HTML 如何“重新显示”被“display: none;”抑制的信息在<style> - How to “re-display” the information suppresses by “display: none;” in <style>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM