简体   繁体   English

切换页面时保存功能

[英]save functions when switch pages

I have these two simple functions that change the body background color 我有这两个简单的功能可以更改主体背景色

<script type="text/javascript">
function lightson(){
    document.body.bgColor="#EBEBEB";
}
function lightsoff(){
    document.body.bgColor="#333333";
}
</script>

using links with onclick to exe the functions 使用带有onclick的链接来执行功能

<div style="color:red;">Lights: <a onclick="lightson();" href="#lOn">On</a> - <a onclick="lightsoff();" href="#lOff">Off</a></div>

these append the urls, but when I change them myself to #lOff or #lOn they do nothing, how do I save what the user clicked #lOff or #lOn when they click a page on the site? 这些附加网址,但是当我自己将#lOff更改为#lOff#lOn它们什么也不做,当用户单击站点上的页面时,如何保存用户单击#lOff#lOn的内容?

I use <body onload="lightson();"> to set the background color on index.php, otherwise it goes to default 我使用<body onload="lightson();">来设置index.php的背景色,否则将默认

To get the url part after the hashtag use 使用井号后要获取网址部分

window.location.hash

https://developer.mozilla.org/en/window.location https://developer.mozilla.org/en/window.location

You could call a function that would check the value of the hash on page load to determine which background colour to use. 您可以调用一个函数,该函数将在页面加载时检查哈希值,以确定要使用的背景色。 I'd go for something like this. 我会喜欢这样的东西。

window.onload = function(){
   var hash = window.location.hash;

   if(hash == "#lOn"){
      lightson();
   }else{
      lightsoff();
   }
}

try some thing like this 尝试这样的事情

<script type="text/javascript">
function lightson(attr){
    console.log(attr); // '#lOn'
    document.body.bgColor="#EBEBEB";
}
function lightsoff(attr){
    console.log(attr); // '#lOff'
    document.body.bgColor="#333333";
}
</script>

<div style="color:red;">Lights: <a onclick="lightson('#lOn');" href="#lOn">On</a> - <a onclick="lightsoff('#lOff');" href="#lOff">Off</a></div>

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

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