【问题标题】:Remember toggleClass state in local storage after page refresh JQuery页面刷新 JQuery 后记住本地存储中的 toggleClass 状态
【发布时间】:2016-08-23 21:30:52
【问题描述】:

我试图在页面刷新/页面加载后保留 toggleClass 值。但它不起作用。我有一个分层表,单击时会在其中切换行。我使用localStorage 来保留切换值,但是当页面刷新时,它不记得以前的状态了。

我的点击事件是 -

$('tr.' + 1).toggleClass('hidden'); // Class value I am getting dynamically
localStorage.setItem('hdnvalue', 'hidden'); 

在文档中准备好 -

$(document).ready(function () {    
    if (localStorage.getItem('hdnvalue') == 'hidden') {
        alert(localStorage.getItem('hdnvalue'));
        $('tr.' + 1).toggleClass('hidden');  // I am getting class value dynamically, here 1 is the class level
    }
});

更新:

HTML 示例 -

<table id="tbl_test">
  <tr class="1">
    <td>Row 1</td>
  </tr>
  <tr class="2">
    <td>Row 1.1</td>
  </tr>
  <tr class="3">
    <td>Row 1.1.1</td>
  </tr>
  <tr class="3">
    <td>Row 1.1.2</td>
  </tr>  
  <tr class="1">
    <td>Row 2</td>
  </tr>
  <tr class="2">
    <td>Row 2.1</td>
  </tr>
  <tr class="3">
    <td>Row 2.1.1</td>
  </tr>
  <tr class="3">
    <td>Row 2.1.2</td>
  </tr>
</table>

更新 - JSfiddle demo

如果我隐藏任何行并单击刷新,它不会记住切换状态。我的代码有什么问题。

谢谢

【问题讨论】:

  • 你能正确获取 localStorage.getItem('hdnvalue') 吗?这个$('tr.' + 1) 是否按预期找到了元素?另外,为什么alert(localStorage.getItem('hidden')); ?这应该会提示 null,对吧?
  • @mkaran,是的,我隐藏了 getitem 值,对不起,这是一个错误,我现在正在纠正
  • 你能发一份你的html样本吗?
  • 你有没有像这样在你的css中设置.hidden类:.hidden{ display:none; }

标签: javascript jquery asp.net-mvc


【解决方案1】:

您的代码似乎可以正常工作:

$(document).ready(function() {
  $('#tg1').click(function() {
    $("#result").html("toggled");
    localStorage.setItem('hdnvalue', 'hidden');
  });

  $('#tg2').click(function() {
    $("#result").html("not toggled");
    localStorage.setItem('hdnvalue', 'another');
  });

  if (localStorage.getItem('hdnvalue') == 'hidden') {
    $("#result").html("toggled");
    $('tr.' + 1).toggleClass('hidden');
  }
});

https://jsfiddle.net/qj7o4zug/

可能你使用的浏览器不支持Storage api

if (typeof(Storage) !== "undefined") {
    // Code for localStorage/sessionStorage.
} else {
    // Sorry! No Web Storage support..
}

您是否在控制台中遇到了一些错误?

编辑

这里是固定的代码:

//$(document).ready(function() {
$('#reset').click(function(event) {
  $('tr').each(function(i, el) {
    localStorage['hdn_hiding' + i] = 'show';
  });
});

$('tr').click(function(event) {
  event.stopPropagation();
  var m_indx = $(this).index();
  var currentLevel = parseInt($(this).attr('class')),
    state = $(this).hasClass('hiding'),
    nextEl = $(this).next(),
    nextLevel = parseInt(nextEl.attr('class'));
    debugger;
  while (currentLevel < nextLevel) {
    nextEl.toggle(state);
    nextEl = nextEl.next();
    nextLevel = parseInt(nextEl.attr('class'));
  }

  var $item = $(this).closest('tr');
  var index = m_indx;

  $item.toggleClass('hiding');
  if ($item.hasClass('hiding')) {
    localStorage.setItem('hdn_hiding' + index, 'hiding');
  } else {
    localStorage.setItem('hdn_hiding' + index, 'show');
  }

});

if (typeof(localStorage) == 'undefined') {
  alert('Your browser does not support HTML5 localStorage. Try upgrading.');
} else {
  $('tr').each(function(i, el) {
    var r = localStorage['hdn_hiding' + i] == 'hiding';
    if (r) {
      var currentLevel = parseInt($(this).attr('class')),
        nextEl = $(this).next(),
        nextLevel = parseInt(nextEl.attr('class'));
      while (currentLevel < nextLevel) {
        nextEl.hide();
        nextEl = nextEl.next();
        nextLevel = parseInt(nextEl.attr('class'));
      }      
    } 
  });
}
//});

http://jsfiddle.net/ctuq3z1o

【讨论】:

  • 我正在使用当前的 Chrome 浏览器,显示控制台错误 - 'VM1383:5 Uncaught TypeError: Cannot read property 'removeAttribute' of null'
  • 虽然我是动态获取类值,但是当我添加 alert('tr.'+lvl) 时,我总是得到相同的值,任何建议
  • 嗨 Quentin,我的问题是它一直在获取相同的表行值,我该如何更改它,因为我需要在隐藏类的位置保留隐藏行,谢谢
  • 你能提供一个小 jsfiddle 来展示你的问题吗?
  • 嗨 Quentin,对不起,我工作没了,请查看更新后的问题和 jsfiddle
【解决方案2】:

localStorage 使用的当前代码实现应该可以工作:

localStorage.setItem('hdnvalue', 'hidden');

刷新页面然后:

localStorage.getItem('hdnvalue') \\hidden

localStorage.getItem('hdnvalue') == 'hidden'\真

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多