javascript - 我如何使用 JavaScript 从本地文件读取 CSS 规则

标签 javascript jquery html css

我想做的是读取 css 文件(本地文件)中的内容 但是

  • 我不能使用 GET,因为我不使用服务器,我也不想
  • 我不想允许 chrome --allow-file-access-from-files
  • 我有一个错误,无法使用协议(protocol)文件获取文件://

我的目标是保存一个 html 文件,但在此之前从链接样式表中获取 css 并将其添加到 html 文件之间

css=' <style>\n'
    //+ get the css
    + '</style>\n';

最佳答案

您的问题的解决方案是:

<style>
@import url("style.css");
</style>

链接样式表的经典方式。

JQuery 方式:

<!doctype html >
<html>
    <head>
       <noscript>
          <link rel="stylesheet" href="test.css" />
       </noscript>
    </head>
    <body>

        <div></div>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.js"></script>
        <script>
        $(document).ready(function() {

            $.when($.get("test.css"))
            .done(function(response) {
                $('<style />').text(response).appendTo($('head'));
                $('div').html(response);
            });
        })
        </script>
    </body>
</html>

关于javascript - 我如何使用 JavaScript 从本地文件读取 CSS 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49898685/

相关文章:

css - Targeting::selection 在 jQuery 中用于输出?

javascript - 日期选择器对所有字段使用下拉菜单 JavaScript 和 JQuery W/O 日历

JavaScript 图像淡出和淡入(仅使用 JavaScript,不使用 jQuery)

javascript - 获取加载页面的时间戳

javascript - 使用 JavaScript 获得最高 margin

javascript - 单击父菜单时如何将 + 更改为 -

html - 将列表与另一个元素内联

javascript - Firefox 不显示 Colorbox 照片

javascript - 在特定日期和时间突出显示下一个 <li>

php - 在 javascript 中通过 window.location 传递数据