本文介绍了保持网站主题相同..饼干?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我需要使用javascript来做这个,所以我把它放在堆栈溢出。所以,我的网站有两个主题,唯一的区别是一个是纯色(背景),另一个是重复的图像,所以我没有为它们制作单独的css文件。我在导航栏中有两个链接可以更改它(使用javascript)。在css文件中它是纯色,所以当页面加载时,它就像那样开始。单击图像主题链接时,它会将document.body.backgroundImage设置为图像,当按下纯色主题链接时,它只将背景图像设置为(空),这样您就可以再次看到颜色。那么我怎样才能使主题持久化,而不是在用户转到另一个页面时以及何时返回另一个页面时更改主题。谢谢。

I think I will need to use javascript to do this so I put it here on stack overflow. So, I have two themes for my website, the only difference is that one is a solid colour (background), the other a repeated image, so I didn't make separate css files for them. I have two links in the navigation bar that change it (with javascript). In the css file its the solid colour, so when ever the page loads it starts out as that. When the image theme link is clicked, it sets the document.body.backgroundImage to the image, and when the solid colour theme link is pressed it just sets the background image to "" (empty), so that you can see the colour again. So how can I make the theme persistent, not changing when ever the user goes to another page, as well as when they return another time. Thanks.

编辑:我可以使用PHP或javascript。

I can use either PHP or javascript.

推荐答案

如果您使用的是所有javascript并且没有任何服务器端代码可以使用,这里有一个设置和读取cookie的JS示例:

If you're using all javascript and don't have any serverside code to work with, here's a JS example to set and read a cookie:

添加此功能到你的JS,然后在主题改变时运行它:

Add this function to your JS, then run it when the theme is changed:

function set_theme(name){
  document.cookie='sel_theme='+name+';';
  }//so, run set_theme('whatevername'); when it is set by the user

读取cookie并在页面加载时设置主题(使用jQuery或者类似的$(document).ready()会比onload更好,但是这里是一个直接的js / dom示例)

To read the cookie and set the theme on page load (using jQuery or similar $(document).ready() would be better than onload, though, but here's a straight js/dom example)

 window.onload=function(){
var cookie_pos=document.cookie.indexOf('sel_theme=');//locate value in cookie
if(cookie_pos!=-1){//if string was found
  var cookie_flavor=substr(cookie_pos+10,document.cookie.indexOf(';',cookie_pos));//extract the value of the cookie
  /*then run your already existing change theme function using the extracted name*/
  }    

这篇关于保持网站主题相同..饼干?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:46