简体   繁体   English

即使页面刷新也如何保持.current类

[英]How to keep .current class even if page refresh

I have following script that adds current class to <li> in a Joomla based website: 我有以下脚本,用于在基于Joomla的网站中将当前类添加到<li>中:

$(function(){
  $("#slide-menu > li ").click(function(e){
    e.preventDefault();
    $("#slide-menu > li ").addClass("current").not(this).removeClass("current");
  });
});

But when I click on the link page refresh and current class disappear Here is my html: 但是,当我单击链接页面刷新时,当前类消失了,这是我的html:

<ul id="slide-menu" class="menu menu-sidebar">
  <li class="level1 item187">
    <a class="level1" href="/plastikovye-okna-i-dveri/other/plastikovye-okna-i-dveri">
     <span>Пластиковые окна</span>
    </a>
  </li>
  <li class="level1 item188">
    <a class="level1" href="/plastikovye-okna-i-dveri-2/uncategorised/okna-rehau">
     <span>Входные двери</span>
    </a>
  </li>
</ul>

after the page refresh it will load everything again, so you can't keep it , but you can follow one of those three methods to achieve this: 页面刷新后,它将再次加载所有内容,因此您无法保留它 ,但是您可以按照以下三种方法之一来实现:

  1. sending an ajax call to the server and save it there 将ajax调用发送到服务器并将其保存在服务器上
  2. save the value to cookies and in your script when it loads it should check if that cookie exists and load the value from it 将值保存到cookie中,并在加载脚本时在脚本中检查该cookie是否存在并从中加载值
  3. match the current selected elements from the requested URL. 与请求的URL中当前选择的元素匹配。

Good luck 祝好运

You can use two things to storage this kind of things. 您可以使用两件事来存储这种事情。 LocalStorage or in a Session, if you want to keep it fully client side I should use LocalStorage. LocalStorage或会话中,如果要完全保留它在客户端,我应该使用LocalStorage。

LocalStorage 本地存储

With localstorage you can save the currentClass. 使用localstorage可以保存currentClass。 This prevents you from using serverside code. 这样可以防止您使用服务器端代码。

A little example: 一个小例子:

/* Set localStorage item */
localStorage.setItem('name', 'value');

/* Get localStorage item */
localStorage.alpha;

/* Or */
localStorage['alpha'];

When you saved the class with is current in the localStorge you can add it on the document ready. 当使用class在localStorge中保存当前类时,可以将其添加到文档中。

Session Storage 会话存储

Another way to store it is in an session. 存储它的另一种方法是在会话中。 This requeres a request to your server. 这将请求重新发送到您的服务器。 Best thing to do this is with Ajax ( I use the jquery lib for this ). 最好的方法是使用Ajax(为此我使用了jQuery lib)。

A little example: 一个小例子:

Set Session 设置会话

/*  Client side */
function setSession($key, $value) {
   $.ajax({
     type: "POST",
     url: "setSession.php",
     data: { key: "Foo", value: "Bar" }
    });
}

/* Server side (PHP example) */
<?php
  /* secure it your self. */
  $_SESSION[$_POST['key']] = $_POST['value'];
?>

Get Session 取得会议

/*  Client side */
function getSession($key) {
   $.ajax({
     type: "POST",
     url: "getSession.php",
     data: { key: "Foo" }
    }).done(function( callback ) {
      return callback ;
    });
}

/* Server side (PHP example) */
<?php
  /* secure it your self. */
  echo $_SESSION[$_POST['key']];
?>

Use the function 使用功能

var foo = getSession("foo");

Check the currentClass and do your trick ;) 检查currentClass并做你的把戏;)

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

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