在PHP中将类添加到Body并与会话保持联系 [英] Adding a class to Body in PHP and keeping it with a session

查看:69
本文介绍了在PHP中将类添加到Body并与会话保持联系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图自己解决这个问题,但是我似乎无法对其进行排序.

I've been trying to figure this out on my own, but I can't seem to get it sorted.

我正在客户端站点上构建一个可访问性部分,我有两个按钮,这些按钮为主体添加了一个类,一个是font-size另一个是greyscale.

I'm building an accessibility section on a client site, and i've got two buttons, the buttons add a class to the body, one is font-size the other is greyscale.

我需要这些类保留在主体上,直到再次单击以将其删除为止,因为用户并不需要一直单击按钮才能看到该站点.

I need these classes to stay on the body until clicked again to remove, as users don't want to have to keep clicking the buttons to be able to see the site.

我想将这些类与会话或cookie存储在一起,但是在阅读后,会话仍然存储cookie,因此以最佳选择为准.

I want to store these classes with a session or cookie, but having done some reading, sessions store cookies anyway, so whichever is the best option.

我在网站上使用 wordpress ,因此,如果有什么我可以明智地使用函数的方法,那将很有用!

I'm using wordpress for the site, so if there's something I can use function wise, that'd be useful to know!

有人可以帮我吗?

推荐答案

如果要使用localStorage,可以使用此代码.

If you want to use localStorage you can use this code.

// Check if localStorage is supported
if ('localStorage' in window && typeof localStorage == 'object') {
  $(document).ready(function() {
    // Set the class if greyscale is set
    // Note that localStorage saves everything as strings
    if (localStorage["greyscale"] == "1") {
      $('body').addClass('greyscale');
    }
    // Register click listener for the button
    $('#button').click(function() {
      // Toggle greyscale on and off
      if (localStorage["greyscale"] != "1") {
        $('body').addClass('greyscale');
        localStorage["greyscale"] = "1";
      }
      else {
        $('body').removeClass('greyscale');
        localStorage["greyscale"] = "0";
      }
    }); // - button click
  }); // - doc ready
}

JSFiddle

这篇关于在PHP中将类添加到Body并与会话保持联系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆