Automatically inline CSS from a CSS-file (jquery? ajax? php?)

自古美人都是妖i 提交于 2019-12-13 23:23:11

问题


For reasons we dont need to get in to, I need to put the content of a CSS-file into the style tags of a html-page. I want to do it automatically, so I need to read the CSS file and put it's content in my style tag in my header.

Any idea how to do this?


回答1:


The best would be if you used PHP, django, ASP.net or something from this family. For PHP I would do like this:

<style id="Something">
<?php readfile("http://example.com/some/style.css"); ?>
</style>

But if you want to use jQuery, try this:

<style id="Something"></style>
<script type="text/javascript">
var request = $.ajax({
  url: "stylesheet.css",
});
request.done(function( msg ) {
  $( "style#Something" ).html( msg );
});
request.fail(function( jqXHR, textStatus ) {
  alert( "Request failed: " + textStatus );
});
</script>


来源:https://stackoverflow.com/questions/25260263/automatically-inline-css-from-a-css-file-jquery-ajax-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!