需要修改这个函数,它返回所有样式完好无损的DOM元素的代码

Need to modify this function, which returns code for any DOM element with all styles intact

本文关键字:完好无损 样式 DOM 代码 元素 返回 修改 函数      更新时间:2023-09-26

我在StackOverflow上发现了这个精彩的脚本,它返回所有样式完好无损的任何DOM元素的代码:https://stackoverflow.com/a/6310120/3001859

唯一的问题是,(看起来)它不计算并返回原始元素的样式。它只对内部元素做这个。我如何修改这个-所以它返回完整的代码,与原始元素的样式?

(我不能评论原来的线程,因为我的低代表)

递归调用的问题是它必须有开始和结束的情况。不需要做很多工作,你就可以通过为所选元素创建父元素并使用它来实现这一点。

/*grab the page*/
var orgin = $('body').html();
/*grab the contents you are looking for, then replace the body*/
$('body').html($('widgets'));
/*get the widgets style*/
var htm = $('body')[0].serializeWithStyles();
/*give the body back to origin*/
$('body').html(orgin);
/*append the results*/
$('prepend').html($("<div>").text(htm).html())
http://jsfiddle.net/1vv8nx1u/2/

如果不需要,可以去掉body标签,这是一个简单的解析。