虽然会有很多其他的方式来处理你的情况。但如果你真的想这样做。试试这个代码,但它的工作,但如果你想要任何增强,你可以轻松地进一步。
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<html>
<head>
<title>test</title>
</head>
<body>
<div style="color:black;">
<div style="color:blue;">abc</div>
<div>Xyz</div>
1111
</div>
</body>
</html>
<script type="text/javascript">
allstyle = "";
classNames = "";
count = 1;
$("*").each(function() {
tag = $(this);
$.each(this.attributes, function() {
if(this.name == 'style'){
allstyle += this.value;
tag.removeAttr("style");
tag.addClass("custom-class"+count);
classNames +=" .custom-class"+count+"{"+allstyle+"}";
count++;
}
});
});
$("<style>").html(classNames).appendTo("head");
</script>
原因,你不能只使用相同的类名吗? – gurvinder372
看到[XY问题](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)... –
如果你有颜色:黑色;在一种风格属性和颜色中:蓝色;在另一个 ?喜欢这个? – C2486