简体   繁体   English

有没有一种方法可以根据其他页面的样式表来更改页面的样式表?

[英]Is there a way of changing a page's stylesheet based on the stylesheet of a different page?

I'm making a webpage which includes buttons to change the theme from light to dark and back on the front page. 我正在制作一个网页,其中包含将主题从浅色更改为深色然后在首页上更改的按钮。 However, if I click through to another page I want the theme to stay. 但是,如果我单击进入另一页面,则希望保留该主题。

Is there a way I can tell the browser to choose the css file dynamically based on whether the user was using the light or dark theme? 有没有一种方法可以告诉浏览器根据用户使用的是亮还是暗主题来动态选择css文件?

For reference here is what I have on the front page: 供参考的是我在首页上的内容:

Javascript: 使用Javascript:

function swapStyleSheet(sheet, text){
//variables to access elements in the DOM
var styleSheet = document.getElementById('pagestyle');
var defaultText = document.getElementById('styleSwitchText');

styleSheet.setAttribute('href', sheet); //changes the stylesheet
defaultText.style.display = "none"; //hides initial text

//creates p element and appends text parameter to it
var pElement = document.createElement('p');
var textnode = document.createTextNode(text); 
pElement.appendChild(textnode);

var item = document.getElementById('swapStyleButtons');
//replaces the text node in the p each time button is clicked
item.replaceChild(pElement, item.childNodes[0]);
}

html: HTML:

<div id="swapStyleButtons">
    <p id="styleSwitchText">Don't like the style? Try Night Mode!</p>
    <button id='night' onclick='swapStyleSheet("dark.css", "Think Night Mode sucks? Switch back!")'>Night</button>
    <button id='default' onclick='swapStyleSheet("recipes.css", "Liked it better before? Try Night Mode!")'>Default</button>
</div>

This works perfectly on the front page. 这在首页上效果很好。 I could just repeat this process for the other pages but then the user would have to press the button on every page, which is obviously a bit annoying. 我可以对其他页面重复此过程,但是用户必须按每页上的按钮,这显然有点烦人。

Can 1 html page communicate with another in this way? 1个html页面可以通过这种方式与另一个进行通信吗?

With HTML5 you can store data with HTML itself and u do not need to another language. 使用HTML5,您可以使用HTML本身存储数据,而您无需使用其他语言。

function setColorTheme(curColorTheme)
{
    if(typeof(Storage) !== "undefined") {
        localStorage.setItem("colorTheme", curColorTheme);
    } else {
        // Backup cookie Support
    }
}

function getColorTheme()
{
    if(typeof(Storage) !== "undefined") {
        return localStorage.getItem("colorTheme");
    } else {
        // Backup cookie Support
    }
}

Here is a tutorial: 这是一个教程:

http://www.w3schools.com/html/html5_webstorage.asp http://www.w3schools.com/html/html5_webstorage.asp

Inside buttons click event for choosing stylesheet write following code to set selected stylesheet as local storage 内部按钮单击事件以选择样式表,编写以下代码以将所选样式表设置为本地存储

localStorage.setItem("Selected_Style_sheet_name", "yourstylesheetname");

In Home Page set selected theme 在首页中设置所选主题

 var btn1 = document.getelementbyid("btnTheme1");
    var btn2 = document.getelementbyid("btnTheme2");

    btn1.onclick =function(){
    localStorage.setItem("Selected_Style_sheet_name", "Theme1.css");

    }
    btn2.onclick =function(){
    localStorage.setItem("Selected_Style_sheet_name", "Theme2.css");

    }

Now apart from home page every page add following code it 现在除了主页之外,每个页面都添加以下代码

if(localStorage.getItem("Selected_Style_sheet_name") !== null){

    styleSheet.setAttribute('href', localStorage.getItem("Selected_Style_sheet")); 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM