简体   繁体   English

Cookie 更改/切换/切换 CSS 类 jQuery 问题

[英]Cookie change/switch/toggle CSS class jQuery problems

I found this neat solution for changing background color and storing it into cookie , using jquery.cookie .我找到了这个使用jquery.cookie 更改背景颜色并将其存储到 cookie 的巧妙解决方案 Made a little adjustment and it works great:做了一点调整,效果很好:

$(document).ready(function () {
 $("body").css("background-image",$.cookie("<?php echo $_SESSION['username_login']; ?>"));
     $("#background-change").change(function (event) {
       var img =  $(this).val();
        $("body").css("background-image",img);
         $.cookie("<?php echo $_SESSION['username_login']; ?>",img, {path: '/', secure: true});
     });
 });

Now I am trying based on this make a theme color change witch has default class .w3-blue-grey.现在我正在尝试基于此使主题颜色更改女巫具有默认类 .w3-blue-grey。 This is closest that I got:这是我得到的最接近的:

$(document).ready(function () {
$(".w3-blue-grey").toggleClass($.cookie("<?php echo $_SESSION['username_login']; ?>col"));
    $("#color-change").change(function (event) {
      var col =  $(this).val();
       $(".w3-blue-grey").toggleClass(col);
        $.cookie("<?php echo $_SESSION['username_login']; ?>col",col, {path: '/', secure: true});
    });
});

As you can see I made the name of the cookie differently so there is no collision there but for some reason this code is not working.正如您所看到的,我以不同的方式创建了 cookie 的名称,因此那里没有冲突,但由于某种原因,此代码不起作用。

I can not even explain what is the error, it's like it has mind of his own, sometimes it changes into color, but wrong one, then won't go back to default, sometimes it gives no class at all and it's transparent... I just don't get it...我什至无法解释错误是什么,就像它有自己的想法,有时它会变成颜色,但错误的,然后不会回到默认状态,有时它根本不提供任何类并且它是透明的.. . 我就是不明白...

I tried with switchClass instead of toggle but it was also going wild.我尝试使用switchClass而不是切换,但它也很疯狂。

I noticed it may have something to do with order of select (sometimes when I do changes in it) so I'm giving my HTML code too.我注意到它可能与选择顺序有关(有时当我对其进行更改时),所以我也提供了我的 HTML 代码。

<select name="wall" id="background-change" class="w3-padding">
   <option>...</option>
   <option value='url("img/wall6.png")'>Glavna</option>
   <option value='url("img/wall2.png")'>Opcija 1</option>
   <option value='url("img/wall3.png")'>Opcija 2</option> 
</select>

Up is my select for changing background and that works. Up 是我选择更改背景的方法,效果很好。 And below is my theme switcher:下面是我的主题切换器:

<select name="colour" id="color-change" class="w3-padding">
        <option >...</option>
        <option value='w3-blue-grey'>Light grey</option>
        <option value='w3-indigo'>Indigo</option>
        <option value='w3-blue'>Light blue</option>
</select>

I lost hours on this and I don't see what else I could think of to solve it, please advise.我在这个问题上浪费了几个小时,我看不出我还能想到什么来解决它,请指教。

I sorted it out.我整理了一下。 Anyone has better solution maybe?有人有更好的解决方案吗?

<script type="text/javascript">
       $(document).ready(function () {
        $(".w3-blue-grey").css('cssText',$.cookie("<?php echo $_SESSION['username_login']; ?>col"));
            $("#color-change").change(function (event) {
 var col =  $(this).val();
  //window.alert(col);
  $(".w3-blue-grey").css('cssText',col);
   $.cookie("<?php echo $_SESSION['username_login']; ?>col",col, {path: '/', secure: true});
   //location.reload();
            });
        });
</script>    

I sorted it out... switching classes didn't work properly for some reasons so now I am just making changes to style the class produces.我解决了...由于某些原因,切换类无法正常工作,所以现在我只是对类产生的样式进行更改。 The value in HTML select needs to be full style that you wish to apply. HTML select 中的值需要是您希望应用的完整样式。 and then pass it into cssText .然后将其传递给cssText

       $(document).ready(function () {
        $(".w3-blue-grey").css('cssText',$.cookie("<?php echo $_SESSION['username_login']; ?>col"));
            $("#color-change").change(function (event) {
 var col =  $(this).val();
  //window.alert(col);
  $(".w3-blue-grey").css('cssText',col);
   $.cookie("<?php echo $_SESSION['username_login']; ?>col",col, {path: '/', secure: true});
   //location.reload();
            });
        });

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

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