我陷入了一种情况,我只能访问网站的主体,而不是头部.我必须使用新的样式表.现在我遇到的解决方案是在网站正文中添加CSS文件.当然,这是一个黑客,所以我想知道是否有更好的解决方案呢?
wel*_*ell 34
在页面上加载外部CSS文件的传统方法是:
<head>
<link rel="stylesheet" type="text/css" href="file.css" />
</head>
Run Code Online (Sandbox Code Playgroud)
只使用JavaScript来创建一个Javascript函数:
<script type="text/javascript">
function loadCSS(filename){
var file = document.createElement("link");
file.setAttribute("rel", "stylesheet");
file.setAttribute("type", "text/css");
file.setAttribute("href", filename);
document.head.appendChild(file);
}
//just call a function to load your CSS
//this path should be relative your HTML location
loadCSS("path_to_css/file.css");
</script>
Run Code Online (Sandbox Code Playgroud)
您可以添加动态定义,例如:
<script type="text/javascript">
var sheet = (function() {
var style = document.createElement("style");
style.appendChild(document.createTextNode(""));
document.head.appendChild(style);
return style.sheet;
})();
sheet.insertRule("span { visibility: hidden }", 1);
</script>
Run Code Online (Sandbox Code Playgroud)
Sim*_*bæk 10
关于什么:
$('head').append('<link rel="stylesheet" type="text/css" href="{yoururl}">');
Run Code Online (Sandbox Code Playgroud)
你的意思是再次定义CSS并覆盖以前的CSS吗?:
?<html>
<head>
<style type='text/css'>
* {color:red;}
p {background-color:yellow;}
</style>
</head>
<body>
<style type='text/css'>
* {color:green;}
p {background-color:black;}
</style>
<p>"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..." </p>
"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."
<p>"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..." </p>
</body>
</html>????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)
你可以在那里复制整个样式表,当然然后用php或javascript包含它.但是这样,查看头部CSS样式表并覆盖正文中出现的所有样式都应该有效.不知道这是否干净.
您可以使用该@import url("your_styles.css");
方法。
如果您有权访问文档开头的样式表,则可以将其添加到CSS文档的顶部。
您也可以尝试向<head>
文档中添加替代项,我不建议这样做,但是如果需要,您也可以这样做:
<style type="text/css">
@import url("your_style.css");
</style>
Run Code Online (Sandbox Code Playgroud)
如果您不关心向后兼容性,那么scoped
此问题中也解决了HTML5 属性:<STYLE>是否必须位于HTML文档的<HEAD>中?
希望这可以帮助!
编辑:
找到了有关@import功能的两个链接。一个是来自Mozilla开发人员中心的工作草案,该草案于2012年7月31日进行了最后更新:
https://developer.mozilla.org/zh-CN/docs/CSS/@import
另外还有有关浏览器支持统计信息的《站点点参考》文章:
http://reference.sitepoint.com/css/at-import
我想如果需要的话,这仍然是一个功能可用的功能。
归档时间: |
|
查看次数: |
59364 次 |
最近记录: |