0

我有一个页面,其中包含几个隐藏的 div 和带有单击事件的按钮,其中 div 变得可见。

问题是当我重新加载页面时,所有 div 都再次隐藏。

实际上,当我单击一些按钮时,一些 div 变得可见,但是当我重新加载页面时,所有 div 都再次不可见。

我可以使页面重新加载之前可见的 div 在重新加载后可见吗?

示例http://jsfiddle.net/DxKCj/1/.

4

3 回答 3

1

Of course they will be invisible again. That is how HTML/JavaScript is supposed to behave. When you reload the page, the browser throws away the current state and "compiles" the page all over again. It is like running running a simple C program, exiting it, and running it again. If you want to save the state of the Divs, you will have to save it somewhere. You can either save it using HTML5 local storage, or in the cookies. You can do both of these through JavaScript.

于 2013-02-20T16:03:56.723 回答
1

是的。不要打扰使用会话 - 只需设置一个 cookie。谷歌“javascript setcookie”,你会找到你需要的一切——存储哪些 div 在 cookie 中可见,然后在页面加载时检查 cookie 值。`

于 2013-02-20T16:04:51.143 回答
0

Check out the following answer:

create session with jquery?

You can make an ajax call to a PHP page which will create a session. You can then do a simple IF when the page loads to see if the session is set to 1 or 0. If it's 1, display the div, if not, remove it.

Simply make the AJAX call when the user clicks the button. If you need more specific code, let me know

于 2013-02-20T16:02:38.207 回答