【问题标题】:Local storage Jquery not working after visit?访问后本地存储Jquery不起作用?
【发布时间】:2014-04-02 23:43:03
【问题描述】:

这是我的HTML

<div class="col-md-9">
  <table class="table table-striped table-bordered table-condensed">
    <thead>
      <tr style="background-color: white">
        <th> <i class="fa fa-male"></i> Name </th>
        <th><i class="fa fa-bars"></i> Particular</th>
        <th><i class="fa fa-envelope-o"></i>Unit</th>
        <th><i class="fa fa-map-marker"></i>Quantity</th>
        <th><i class="fa fa-phone"></i>From</th>
        <th><i class="fa fa-phone"></i>Date</th>
        <th><i class="fa fa-phone"></i>TAKE actions</th>
      </tr>
    </thead>
    <tbody>

      <tr>
        <td>COMB</td>
        <td>fasionable comb</td>
        <td>SVP</td>
        <td>3</td>
        <td>Bindibhatt1</td>
        <td>2014-03-22 18:15:34 UTC
        </td><td>
          <form action="/requisitions/2" class="button_to" method="get"><div><input class="po" data-confirm="Are you sure?" type="submit" ></div></form>

        </td>
      </tr>
      <tr>
      </tr>
    </tbody>
  </table>

</div>

这是我的JQ

$(window).load(function(){

    $(".po").click(function(){
        localStorage.setItem("visited" + $(this).index(), true);
        $(this).css("color", "red"); // visited
    });

    for(var i = 0, len = $(".po").length; i < len; i++){

        if(localStorage.getItem("visited" + i)){
            $(".po:eq(" + i + ")").css("color", "white"); // visited
        }else
        {
            $(".po:eq(" + i + ")").css("color", "black"); // not visited
        }
    }

});

问题是只能看到第一行的变化,当我添加另一行然后访问它时没有效果

【问题讨论】:

  • 你为什么用$(window).load()而不是更普通的$(document).ready()

标签: jquery ruby-on-rails-4 local-storage visited


【解决方案1】:

只要改变这个:

localStorage.setItem("visited" + $(this).index(), true);

为此:

 localStorage.setItem("visited" + $(this).closest("tr").index(), true);

由于您获得了输入的索引,因此使用它始终为 0,我们将指向输入所在的 closest tr,然后我们获取它的索引。

Fiddle

【讨论】:

  • 您在 localStorage 中只有 "visited0" 有哪些值?尝试调试它并查看 $(this).closest("tr").index() 返回的内容 我在问题上使用了相同的 HTML 是否缺少某些内容?
  • 它在小提琴上工作,但不是由 ruby​​ on rails 应用程序..:(
  • 在 localStorage.setItem 调用之前放置一个调试器并打开你的控制台然后打印 $(this).closest("tr").index() 得到的值可能是 dom 不同,是点击事件触发?
猜你喜欢
  • 2023-04-08
  • 2017-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-13
  • 2015-11-29
  • 2012-11-19
相关资源
最近更新 更多