Javascript 如何防止切换到备用样式表时出现主样式表?
我在header.php中按以下顺序提到了两个样式表:Javascript 如何防止切换到备用样式表时出现主样式表?,javascript,html,css,Javascript,Html,Css,我在header.php中按以下顺序提到了两个样式表: <link rel="stylesheet" type="text/css" title="day" href="<?php echo $_layoutParams['root_css']; ?>day.css"> <link rel="stylesheet" type="text/css" title="night" href="<?php echo $_layoutParams['root_css']
<link rel="stylesheet" type="text/css" title="day" href="<?php echo $_layoutParams['root_css']; ?>day.css">
<link rel="stylesheet" type="text/css" title="night" href="<?php echo $_layoutParams['root_css']; ?>night.css">
您可以通过以下方式使用Jquery禁用/启用CSS文件:
$('link[href="mystyle.css"]').prop('disabled', true);
$('link[href="mystyle.css"]').prop('disabled', false);
“在加载DOM后,我还会在每个页面顶部启动switch_样式的函数”-为什么是在之后而不是之前?可能重复@CarlosAlvesJorge这是一个不同设置的类似问题。您提到的帖子只是关于没有保留cookie状态的实时样式切换。@Andreas想在DOM之前演示如何加载它吗?感谢您添加了代码,以便在DOM完全加载后执行您的函数—这是必要的,因为从\u cookie()调用set\u style\u的
标记位于DOM之前。因此。。。
function switch_style ( css_title )
{
var i, link_tag ;
for (i = 0, link_tag = document.getElementsByTagName("link") ;
i < link_tag.length ; i++ ) {
if ((link_tag[i].rel.indexOf( "stylesheet" ) != -1) &&
link_tag[i].title) {
link_tag[i].disabled = true;
if (link_tag[i].title == css_title) {
link_tag[i].disabled = false;
}
}
set_cookie( style_cookie_name, css_title,
style_cookie_duration, style_domain );
}
}
<script>
document.addEventListener("DOMContentLoaded", function(event) {
set_style_from_cookie();
});
</script>
<link rel="stylesheet" type="text/css" title="night" href="<?php echo $_layoutParams['root_css']; ?>night.css">
<link rel="stylesheet" type="text/css" title="day" href="<?php echo $_layoutParams['root_css']; ?>day.css">
$('link[href="mystyle.css"]').prop('disabled', true);
$('link[href="mystyle.css"]').prop('disabled', false);