如何使用XML将HTMLCollection对象转换为String?

时间:2022-10-24 21:29:36

What is the best way to get the full String representation of an HTMLCollection object in javascript/jquery? The output String should have an XML syntax.

在javascript / jquery中获取HTMLCollection对象的完整String表示形式的最佳方法是什么?输出String应该具有XML语法。

3 个解决方案

#1


$('<div/>').append(HTMLCollection).html(); // Retrieves the "outerHTML"

#2


Considering jQuery .html() and .outerHTML() returns the first matched element of a set of elements you may use .outerHTML(function(index, outerHTML)) from https://github.com/darlesson/jquery-outerhtml to do something like:

考虑到jQuery .html()和.outerHTML()返回一组元素的第一个匹配元素,你可以使用.outerHTML(function(index,outerHTML))来自https://github.com/darlesson/jquery-outerhtml来做就像是:

Consider the following HTML:

请考虑以下HTML:

<span>One</span>
<span>Two</span>
<span>Three</span>

Consider this call:

考虑这个电话:

var allHTML = "";

$("span").outerHTML(function(index, outerHTML){

    allHTML = allHTML + outerHTML;

});

allHTML variable is equal "<span>One</span><span>Two</span><span>Three</span>".

allHTML变量等于“一个 两个 三个 ”。

#3


My guess is to first clone the nodes before trying the div trick:

我的猜测是在尝试div技巧之前首先克隆节点:

$('<div/>').append($(HTMLCollection).clone()).html();

Without cloning, the reassignment of the elements of the collection to the newly created div will fail.

如果没有克隆,将集合元素重新分配给新创建的div将失败。

#1


$('<div/>').append(HTMLCollection).html(); // Retrieves the "outerHTML"

#2


Considering jQuery .html() and .outerHTML() returns the first matched element of a set of elements you may use .outerHTML(function(index, outerHTML)) from https://github.com/darlesson/jquery-outerhtml to do something like:

考虑到jQuery .html()和.outerHTML()返回一组元素的第一个匹配元素,你可以使用.outerHTML(function(index,outerHTML))来自https://github.com/darlesson/jquery-outerhtml来做就像是:

Consider the following HTML:

请考虑以下HTML:

<span>One</span>
<span>Two</span>
<span>Three</span>

Consider this call:

考虑这个电话:

var allHTML = "";

$("span").outerHTML(function(index, outerHTML){

    allHTML = allHTML + outerHTML;

});

allHTML variable is equal "<span>One</span><span>Two</span><span>Three</span>".

allHTML变量等于“一个 两个 三个 ”。

#3


My guess is to first clone the nodes before trying the div trick:

我的猜测是在尝试div技巧之前首先克隆节点:

$('<div/>').append($(HTMLCollection).clone()).html();

Without cloning, the reassignment of the elements of the collection to the newly created div will fail.

如果没有克隆,将集合元素重新分配给新创建的div将失败。