I know I can get the style tag 开发者_Python百科with
$('style')
but how can I get all style tags from the document and merge them into a single style block ?
<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>
Here is how you can do it in two lines:
// 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();
Here is a demo Updated
精彩评论