Javascript 如何分离HTML元素和CSS元素并返回它们?

Javascript 如何分离HTML元素和CSS元素并返回它们?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,这是我想要实现的一部分 这将返回DOM结构,如下所示: <div id="content"> <div id="example1" style="width:100px height:100px background-color:red"></div> <div id="example2" style="width:50px height:50px background-color:blue"></div>

这是我想要实现的一部分

这将返回DOM结构,如下所示:

 <div id="content">
     <div id="example1" style="width:100px height:100px background-color:red"></div>
     <div id="example2" style="width:50px height:50px background-color:blue"></div>
     <div id="example3" style="width:25px height:25px background-color:yellow"></div>
 </div>


我将动态添加
div
s,CSS将
inline
。理想情况下,我希望能够取出CSS,这样我就可以返回
div
元素,然后将CSS属性分别作为文本元素返回。

您可以删除样式属性并将它们存储在一些id样式映射中,以便以后需要时可以使用它们。大概是这样的:

var样式={};
$(“#内容”).children().each(函数()){
样式[this.id]=$(this.attr('style');
$(this.removeAttr('style');
});
//检查
警觉的(
'HTML:\n\n'+
$(“#内容”)[0].outerHTML+
'\n\nCSS:\n\n'+
stringify(样式,null,4)
);


这不是很清楚,但是如果你想删除style属性,你可以做
$(“#content”).children().removeAttr('style')
你能举例说明你需要的输出吗?我根据你的代码行计算出来了,谢谢。我使用了
alert($(“#content”).children().attr(“style”)
 <div id="content">
     <div id="example1" style="width:100px height:100px background-color:red"></div>
     <div id="example2" style="width:50px height:50px background-color:blue"></div>
     <div id="example3" style="width:25px height:25px background-color:yellow"></div>
 </div>