从CSS文件自动内联CSS(jquery?ajax?php?) [英] Automatically inline CSS from a CSS-file (jquery? ajax? php?)

查看:99
本文介绍了从CSS文件自动内联CSS(jquery?ajax?php?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于不必要的原因,我需要将CSS文件的内容放入html页面的样式标签中.我想自动执行此操作,因此需要读取CSS文件,并将其内容放在标头中的样式标签中.

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.

有什么想法吗?

推荐答案

最好的方法是使用PHP,django,ASP.net或该系列产品. 对于PHP,我会这样:

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>

但是,如果您想使用jQuery,请尝试以下操作:

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>

这篇关于从CSS文件自动内联CSS(jquery?ajax?php?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆