得到所有的<style>标签,并将内容连接到一个<style>标签

Get all <style> tags and concatenate the contents into one <style> tag

本文关键字:style 标签 连接 一个      更新时间:2023-09-26

我知道我可以用

获取style标签

$('style')

但是如何从文档中获得所有样式标签并将它们合并到单个样式块中呢?

<script>
var styles;
$("style").each(function(a,b){
    styles = styles + "'n" + b.html();
    $(this).remove();
});
$("#style").html('<style>'+ styles +'</style>');
</script>
<div id="style>
</div>

下面是两行代码:

// add a new style element before the first one with all the rules
$('style:first').before('<style type="text/css" id="new">'+$('style').text()+'</style>');
// remove all the style elements except the one added above
$("style:not('#new')").remove();

这是一个演示<一口>更新