单击链接时使用 Jquery 更新 PHP 会话变量
我有几个 div,用户可以使用 jquery 切换方法最小化或展开它们。然而,当页面刷新时,Div 会返回到默认状态。他们是让浏览器记住 div 最后状态的一种方法吗?
例如,如果我展开 ID 为“my_div”的 div,然后单击页面上的其他内容,然后返回原始页面,我希望“my_div”保持展开状态。
我在想可以为此使用会话变量,也许当用户单击展开/最小化按钮时,可以发送 AJAX 请求并切换会话变量...IDK..有什么想法吗?
I have several divs that a user can Minimize or Expand using the jquery toggle mothod. However, when the page is refreshed the Divs go back to their default state. Is their a way to have browser remember the last state of the div?
For example, if I expand a div with an ID of "my_div", then click on something else on the page, then come back to the original page, I want "my_div" to remain expanded.
I was thinking it would be possible to use session variables for this, perhaps when the user clicks on the expand/minimize button a AJAX request can be sent and toggle a session variable...IDK..any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不需要 ajax 请求,只需将信息存储在 cookie 或本地存储中。
这是一个可以帮助您的库: http://www.jstorage.info/
一些示例代码(未经测试):
There's no need for an ajax request, just store the information in a cookie or in the localstorage.
Here's a library which should help you out: http://www.jstorage.info/
Some sample code (untested):
如果您不需要支持旧版浏览器,可以使用 html5 Web 存储。
你可以这样做(示例取自 w3schools):
If you don't need to support old browsers, you can use html5 web storage.
You can do things like this (example taken from w3schools):
其他人已经给出了与 cookie 和本地存储 API 相关的有效答案,但根据您对问题的评论,以下是如何将
click
事件处理程序附加到链接:事件处理程序函数将运行每当单击它所附加的元素时。在事件处理程序中,您可以运行任何您喜欢的代码。在此示例中,将向
somewhere.php
触发 POST 请求。Others have already given valid answers related to cookies and the local storage API, but based on your comment on the question, here's how you would attach a
click
event handler to a link:The event handler function will run whenever the element it is attached to is clicked. Inside the event handler, you can run whatever code you like. In this example, a POST request is fired to
somewhere.php
.我有类似的东西,我根据用户登录使用cookie
如果您只想要主 div,请不要使用
*
这里是代码 *
I had something like this and I used cookies based on which user logged in
if you want only the main div don't use the
use
*Here is the code *
看看 JavaScript Cookie 方法,您可以保存 div 的当前状态,并在用户返回站点时恢复它。
有一个很好的 jQuery 插件用于处理 Cookies (http://plugins.jquery.com/project/Cookie)
希望它有帮助
Look about the JavaScript Cookie Method, you can save the current states of the divs, and restore it if the User comes back on the Site.
There is a nice jQuery Plugin for handling Cookies (http://plugins.jquery.com/project/Cookie)
Hope it helps
最终用了这个。很棒的教程。
http://www.shopdev.co.uk/博客/cookies-with-jquery-designing-collapsible-layouts/
Ended up using this. Great Tutorial.
http://www.shopdev.co.uk/blog/cookies-with-jquery-designing-collapsible-layouts/