Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在不重新加载页面的情况下交换主题样式表的优雅方式_Javascript_Jquery_Css_Themes - Fatal编程技术网

Javascript 在不重新加载页面的情况下交换主题样式表的优雅方式

Javascript 在不重新加载页面的情况下交换主题样式表的优雅方式,javascript,jquery,css,themes,Javascript,Jquery,Css,Themes,我正在寻找一种在加载页面时热交换样式表的方法。这是我目前的解决方案,它可以工作,但有一些限制/问题 html标题: <link id="theme" href="themes/theme1.css" rel="stylesheet" type="text/css"> 这种方法的问题是,当调用函数时,更改不是即时的,因为浏览器需要对css文件发出额外的GET请求。另一个问题是,如果用户在使用页面时暂时断开连接,他们将没有主题。使用备用样式表可以使其变得简单(两个主题的示例很简单)

我正在寻找一种在加载页面时热交换样式表的方法。这是我目前的解决方案,它可以工作,但有一些限制/问题

html标题:

<link id="theme" href="themes/theme1.css" rel="stylesheet" type="text/css">
这种方法的问题是,当调用函数时,更改不是即时的,因为浏览器需要对css文件发出额外的GET请求。另一个问题是,如果用户在使用页面时暂时断开连接,他们将没有主题。

使用备用样式表可以使其变得简单(两个主题的示例很简单)


函数主题开关(){
var t1=document.getElementById('theme');
var t2=document.getElementById('alttheme');
t1.disabled=!t1.disabled;
t2.disabled=!t1.disabled;
}
一种更通用的方式,允许任意数量的主题

<link class="theme" href="themes/theme1.css" rel="stylesheet" type="text/css">
<link class="theme" href="themes/theme2.css" rel="alternate stylesheet" type="text/css">
<link class="theme" href="themes/theme3.css" rel="alternate stylesheet" type="text/css">

var currentTheme = 0;
var themes = [].slice.call(document.querySelectorAll('link.theme'));

function themeSwitch() {
    currentTheme = (currentTheme + 1) % themes.length;
    themes.forEach(function(theme, index) {
        theme.disabled = (index !== currentTheme);
    });
}

var-currentTheme=0;
var themes=[].slice.call(document.queryselectoral('link.theme');
函数主题开关(){
currentTheme=(currentTheme+1)%themes.length;
themes.forEach(函数(主题、索引){
theme.disabled=(index!==currentTheme);
});
}
最后,尽管您没有标记jQuery,但在代码中确实使用了jQuery,因此,为了jQuery集:

<link class="theme" href="themes/theme1.css" rel="stylesheet" type="text/css">
<link class="theme" href="themes/theme2.css" rel="alternate stylesheet" type="text/css">
<link class="theme" href="themes/theme3.css" rel="alternate stylesheet" type="text/css">

var currentTheme = 0;
var themes = $('link.theme');

function themeSwitch() {
    currentTheme = (currentTheme + 1) % themes.length;
    themes.each(function(index, theme) {
        theme.disabled = (index !== currentTheme);
    });
}

var-currentTheme=0;
var主题=$('link.theme');
函数主题开关(){
currentTheme=(currentTheme+1)%themes.length;
主题。每个(功能(索引、主题){
theme.disabled=(index!==currentTheme);
});
}
使用备用样式表使其变得简单(两个主题的示例很简单)


函数主题开关(){
var t1=document.getElementById('theme');
var t2=document.getElementById('alttheme');
t1.disabled=!t1.disabled;
t2.disabled=!t1.disabled;
}
一种更通用的方式,允许任意数量的主题

<link class="theme" href="themes/theme1.css" rel="stylesheet" type="text/css">
<link class="theme" href="themes/theme2.css" rel="alternate stylesheet" type="text/css">
<link class="theme" href="themes/theme3.css" rel="alternate stylesheet" type="text/css">

var currentTheme = 0;
var themes = [].slice.call(document.querySelectorAll('link.theme'));

function themeSwitch() {
    currentTheme = (currentTheme + 1) % themes.length;
    themes.forEach(function(theme, index) {
        theme.disabled = (index !== currentTheme);
    });
}

var-currentTheme=0;
var themes=[].slice.call(document.queryselectoral('link.theme');
函数主题开关(){
currentTheme=(currentTheme+1)%themes.length;
themes.forEach(函数(主题、索引){
theme.disabled=(index!==currentTheme);
});
}
最后,尽管您没有标记jQuery,但在代码中确实使用了jQuery,因此,为了jQuery集:

<link class="theme" href="themes/theme1.css" rel="stylesheet" type="text/css">
<link class="theme" href="themes/theme2.css" rel="alternate stylesheet" type="text/css">
<link class="theme" href="themes/theme3.css" rel="alternate stylesheet" type="text/css">

var currentTheme = 0;
var themes = $('link.theme');

function themeSwitch() {
    currentTheme = (currentTheme + 1) % themes.length;
    themes.each(function(index, theme) {
        theme.disabled = (index !== currentTheme);
    });
}

var-currentTheme=0;
var主题=$('link.theme');
函数主题开关(){
currentTheme=(currentTheme+1)%themes.length;
主题。每个(功能(索引、主题){
theme.disabled=(index!==currentTheme);
});
}

可能重复的@Qhuea该问题的解决方案在每次切换主题时都需要GET请求。可能重复的@Qhuea该问题的解决方案在每次切换主题时都需要GET请求。我没有看到jQuery标记,抱歉:pI没有看到jQuery标记,对不起:p