【问题标题】:jQuery: load html file containing css stylesheet using html mimetypejQuery:使用 html mimetype 加载包含 css 样式表的 html 文件
【发布时间】:2014-09-24 04:33:24
【问题描述】:
    $.get("page/test.thml", function(html){
        $("#iframe").contents().find("html").html(html);
    },'html')

test.html 包含 css 样式表。

但我得到Resource interpreted as Stylesheet but transferred with MIME type text/html:

html 加载,但缺少 css 样式。我尝试将 css 文件内容复制粘贴到 html 文件本身,但这并没有解决问题。

html加载时如何让css显示?

【问题讨论】:

  • 添加到 html 文件时是否在样式表链接中添加了 type="text/css"..?

标签: jquery css mime-types


【解决方案1】:

请找到以下解决方案。

<script type="text/javascript">
        $(document).ready(function () {
            // Your HTML Physical file path here// Note: style should have text/css attribute.
            $("#result").load("YourHTML.html", function () {
                alert($("#result").html()); // This is your HTML
            });
        });

    </script>

【讨论】:

  • 这给了我Uncaught Error: Syntax error, unrecognized expression: html&gt;
  • 希望你已经放置了 Scripts/jquery.js 另外,如果可能的话,你可以在这里发布你的 html。我已经在我的系统上进行了相同的测试,它工作正常。
【解决方案2】:

试试这个:

$(document).ready(function () {
  $("#result").load("file.html");
});

它将自动加载您的 html,包括您的 css 样式(带有嵌入式 CSS 的 HTML)。但是如果你在html文件中正确设置css文件(Linked CSS case)的映射地址就没有问题了。

【讨论】:

  • 一模一样Resource interpreted as Stylesheet but transferred with MIME type text/html:
  • 原理是浏览器向服务器发出 HTTP 请求。然后服务器做出 HTTP 响应。如果您已声明 html 标头,则 Content-Type 将识别正文的元素是否为 HTML 标签。如果您已经检查了您的 MIME 类型并且 text/css 已经在 css 上并且它仍然无法正常工作,这意味着您的服务器还有其他一些东西正在使该样式表带有错误的内容类型。我建议您使用浏览器开发工具的“网络”选项卡来检查请求和响应。
  • 您检查过您的 .htaccess 吗?请务必提及此错误只会在子目录中且仅在页面刷新后才给您
猜你喜欢
  • 2017-08-28
  • 1970-01-01
  • 2011-11-08
  • 2018-01-09
  • 1970-01-01
  • 2022-10-01
  • 2014-01-05
  • 2014-11-14
相关资源
最近更新 更多