Javascript代码Cookie保留/保留替代Css

Javascript code Cookie to retain/keep alternative Css

本文关键字:保留 Css Cookie Javascript 代码      更新时间:2023-09-26

能帮忙吗我创建了一个网站,但需要知道如何让用户选择他们想要使用的css。我想我需要使用cookie,但我不是100%确定

这是我的索引页

<html>
<head>
    <title>Internet Privacy</title>
    <link rel="stylesheet" type="text/css" href="styles/styles.css" title="Standard">
    <link rel="alternate stylesheet" type="text/css" href="styles/AStyles.css" title="wacky" />
    <script type="text/javascript">
        function changeCSS(cssFile, cssLinkIndex) {
            var oldlink = document.getElementsByTagName("link").item(cssLinkIndex);
            var newlink = document.createElement("link");
            newlink.setAttribute("rel", "stylesheet");
            newlink.setAttribute("type", "text/css");
            newlink.setAttribute("href", cssFile);
            document.getElementsByTagName("head").item(0).replaceChild(newlink, oldlink);
        }
        document.cookie="CSS="
    </script>
</head>
<body>
    <div class="main_wrapper">
        <div class="header_wrapper">
        <!-- This the Logo and Banner Links-->
        <a href="index.php"><img id="logo" src="images/Logo.jpg" height="200" width="350"></a>
        <a href="http://www.wlv.ac.uk"><img id="banner" src="images/wlvLogo.gif" height="200" width="650">
        </div>
            <div class="menubar">
                <ul id="menu">
                    <!-- This is the Menubar that contains links to the various other pages-->
                    <li><a href="index.html">Home</a></li>
                    <li><a href="about.html">Privacy Laws</a></li>
                    <li><a href="procon.html">Pros and Cons</a></li>
                    <li><a href="wfa.html">Fights for Privacy</a></li>
                    <li><a href="aboutme.html">About Me</a></li>
                </ul>
            </div>
            <div class="content_wrapper">
            <!-- This is the Sidebar -->
                <div class="text">
                <h2>I have created this website for my first assignment.<br>This website is about how private the internet and how companies collect data, and how the data is used.</h2><br>
                <img id="indeximg" src="images/privacy.jpg" height="200" width="500">
                <br><p>Change Css Here <br> 
                <a href="#" onclick="changeCSS('styles/styles.css', 0);">Style 1</a> <br> 
                <a href="#" onclick="changeCSS('styles/AStyles.css', 0);">Style 2</a>
                </div>
            </div>
        <div id="footer"><!-- This is the footer and it will be at the bottom of the page --> 
        <br><p>The website was created by Vincent Priestley at The University of Wolverhampton 2015</p>
        </div>
    </div>
</body>

如果有人能帮忙,我将不胜感激

提前谢谢你Vinni

伙计,我不太清楚你的要求是什么,但一般来说,如果你想存储用户的信息,它不应该在前端完成,我的意思是cookie,本地存储…

保存用户的信息,你需要数据库(甚至文件)和服务器端语言来做它,像php+my-sql, asp.mvc+sql…

使用cookie或任何其他前端技术是不好的,无论如何…