我想做的是读取 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/