从 HTMLDivElement 创建字符串

发布于 2024-10-03 11:06:26 字数 315 浏览 5 评论 0原文

我希望能够从 Javascript HTMLElement 对象创建一个字符串。例如:

var day = document.createElement("div");
day.className = "day";
day.textContent = "Random Text";

现在我们已经创建了 day HTMLDivElement 对象,是否可以将其打印为字符串?例如

<div class="day">Random Text</div>

What I would like to be able to do is create a string from a Javascript HTMLElement Object. For example:

var day = document.createElement("div");
day.className = "day";
day.textContent = "Random Text";

Now we have create the day HTMLDivElement Object is it possible to make it print as a string? e.g.

<div class="day">Random Text</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

鹿港小镇 2024-10-10 11:06:27

如果您也可以直接解析字符串,为什么还要使用 createElement 呢?
例如:var string = '

' + 文本 + '

';

Why would you use createElement if you can also directly parse a string?
Like: var string = '<div class="' + class + '">' + text + '</div>';

起风了 2024-10-10 11:06:27

简单使用函数 outerHTML

var anc = document.createElement("a");
anc.href = "https://developer.mozilla.org?a=b&c=d";
console.log(anc.outerHTML); // output: "<a href='https://developer.mozilla.org?a=b&c=d'></a>"

了解更多

Simple use the function outerHTML

var anc = document.createElement("a");
anc.href = "https://developer.mozilla.org?a=b&c=d";
console.log(anc.outerHTML); // output: "<a href='https://developer.mozilla.org?a=b&c=d'></a>"

know more

惯饮孤独 2024-10-10 11:06:26

Gump 包装器的变体,因为他的实现将目标节点从文档中取出。

function nodeToString ( node ) {
   var tmpNode = document.createElement( "div" );
   tmpNode.appendChild( node.cloneNode( true ) );
   var str = tmpNode.innerHTML;
   tmpNode = node = null; // prevent memory leaks in IE
   return str;
}

在屏幕上打印结果字符串(回复:转义)

var escapedStr = nodeToString( node ).replace( "<" , "<" ).replace( ">" , ">");
outputNode.innerHTML += escapedStr;

注意,诸如“class”、“id”等属性是否正确字符串化是有问题的。

Variant on Gump's wrapper, since his implementation lifts the target node out of the document.

function nodeToString ( node ) {
   var tmpNode = document.createElement( "div" );
   tmpNode.appendChild( node.cloneNode( true ) );
   var str = tmpNode.innerHTML;
   tmpNode = node = null; // prevent memory leaks in IE
   return str;
}

To print the resulting string on screen (re: escaped)

var escapedStr = nodeToString( node ).replace( "<" , "<" ).replace( ">" , ">");
outputNode.innerHTML += escapedStr;

Note, attributes like "class" , "id" , etc being stringified properly is questionable.

我的影子我的梦 2024-10-10 11:06:26

你可以使用这个函数(取自pure.js)

function outerHTML(node){
 return node.outerHTML || new XMLSerializer().serializeToString(node);
}

You can use this function (taken from pure.js)

function outerHTML(node){
 return node.outerHTML || new XMLSerializer().serializeToString(node);
}
も让我眼熟你 2024-10-10 11:06:26

距离上次的回答已经过去了几年。所以这里有一个更简单的方法:
我发现 .outerHTML<现在所有主流浏览器都支持 /a>(请参阅 caniuse)。
您可以使用它轻松获取 JS 元素的 HTML:

// Create a sample HTMLDivElement
var Day = document.createElement("div");
Day.className = "day";
Day.textContent = "Random Text";

// Log the element's HTML to the console
console.log(Day.outerHTML)

这将记录:

Random Text

A few years have passed since the last answers. So here is an easier approach:
I found out that .outerHTML is supported by all major browsers now (see caniuse).
You can use it to get the HTML of an JS element with ease:

// Create a sample HTMLDivElement
var Day = document.createElement("div");
Day.className = "day";
Day.textContent = "Random Text";

// Log the element's HTML to the console
console.log(Day.outerHTML)

This will log: <div class="day">Random Text</div>

沦落红尘 2024-10-10 11:06:26

您可以将该元素包装到另一个元素中并在其上使用 innerHTML

var wrapper = document.createElement("div");
wrapper.appendChild(day);
var str = wrapper.innerHTML;

You can wrap that element into another element and use innerHTML on it:

var wrapper = document.createElement("div");
wrapper.appendChild(day);
var str = wrapper.innerHTML;
世界和平 2024-10-10 11:06:26

您需要创建文本节点来为您创建的元素添加文本,如下所示:

var day = document.createElement("div");
day.className = "day";
// create text node
var txt = document.createTextNode('Random Text');
// add text to div now
day.appendChild(txt);
// append to body
document.body.appendChild(day);

You need to create text node to add text for your created element like this:

var day = document.createElement("div");
day.className = "day";
// create text node
var txt = document.createTextNode('Random Text');
// add text to div now
day.appendChild(txt);
// append to body
document.body.appendChild(day);
七月上 2024-10-10 11:06:26

我的元素是一个带有 element : HTMLDivElement 的对象,所以这对我有用。

console.log(row.element.outerHTML);

如果您只有 HTMLDivElement,那么这应该可以工作:

console.log(row.outerHTML);

My element was a object with element : HTMLDivElement, so this worked for me.

console.log(row.element.outerHTML);

If you have just HTMLDivElement, then this should work:

console.log(row.outerHTML);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文