简体   繁体   English

刷新页面时Jquery重置

[英]Jquery resets when I refresh the page

I have a site that you can specify which items on the page that should be shown using this jquery: 我有一个站点,您可以使用此jquery指定页面上应显示的项目:

$('.container h1 a').click(function () {
    $('.container').not($(this).parents()).hide();
    $(this).css({'text-decoration' : 'none',
                'cursor' : 'default'                    
                });
});

But then when I refresh the page the jquery functions that took place are removed 但是当我刷新页面时,删除了发生的jquery函数

Javascript works on the client side, thanks to the browser. 由于浏览器,Javascript在客户端工作。 It can modify the current page, but any change will be erased as soon as you reload the page (you get its original content from the server). 它可以修改当前页面,但是一旦您重新加载页面(您从服务器获取其原始内容),任何更改都将被删除。 If you need to restore those changes as "user preferences", use PHP cookies to store information about how each user changes his interface. 如果您需要将这些更改还原为“用户首选项”,请使用PHP Cookie存储有关每个用户如何更改其界面的信息。 This way, when you reload the page next time, you can execute the required Javascript functions to restore the user's view. 这样,当您下次重新加载页面时,您可以执行所需的Javascript函数来恢复用户的视图。

You could use Ajax to store the user preferences from Javascript, asynchronously. 您可以使用Ajax以异步方式存储来自Javascript的用户首选项。

http://www.w3schools.com/ajax/ Here is W3Schools' Ajax tutorial, you should learn about it. http://www.w3schools.com/ajax/这是W3Schools的Ajax教程,你应该了解它。 When a user changes his preferences on your page, remember it using a PHP cookie ( http://php.net/manual/en/features.cookies.php ). 当用户更改页面上的首选项时,请使用PHP cookie( http://php.net/manual/en/features.cookies.php )记住它。 When the page is reloaded, check the cookie's content, and execute necessary Javascript functions to restore the user's interface. 重新加载页面时,请检查cookie的内容,并执行必要的Javascript函数以恢复用户的界面。

Edit : Didn't know about http://www.electrictoolbox.com/jquery-cookies/ which allows you to store a cookie from jQuery without using Ajax and additional PHP scripts. 编辑:不知道http://www.electrictoolbox.com/jquery-cookies/ ,它允许您存储来自jQuery的cookie,而无需使用Ajax和其他PHP脚本。 Lars Ebert's solution could be lighter indeed, but the general idea is the same. Lars Ebert的解决方案确实可以更轻松,但总体思路是一样的。

The css-properties are only modified temporarily. 只对css-properties进行临时修改。 They are not permanent and are gone when you refresh the page. 它们不是永久性的,当您刷新页面时它们就消失了。 To remember the hidden elements, you would have to use a cookie. 要记住隐藏的元素,您必须使用cookie。 You can use this jQuery-plugin to store a cookie. 您可以使用此jQuery插件来存储cookie。

In this cookie, you can store the id of the hidden element each time the user clicks to hide one element. 在此cookie中,您可以在每次用户单击以隐藏一个元素时存储隐藏元素的ID。 On page-load, you simply read the cookie and re-hide the elements the user has hidden! 在页面加载时,您只需读取cookie并重新隐藏用户隐藏的元素!

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

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