访问其他页面时的持久内容(持久切换)jQuery

发布于 2024-09-26 20:25:15 字数 645 浏览 2 评论 0原文

当我使用切换或任何其他功能更改网站内容时,不会保存状态。所以,如果这是我的页面:

<div id='test'></div>
<input type='submit' value='test'>
<a href='http://www.stackoverflow.com'>link to elsewhere</a>​

并且这是在我的准备代码中:

$(':submit').click( function() {
  $('#test').text( 'this is a test' );
});

当我单击提交按钮时,div 将填充“这是一个测试”。当我点击 stackoverflow 的链接并单击“返回”时,DIV 将再次为空。单击另一个页面时如何使 DIV 持久存在,就像 元素持久存在的方式一样(即,如果我在 并点击链接,当我单击“后退”时,文本仍然存在)

最终我想保存切换的状态...或者我应该使用 cookie 来解决这个问题,这意味着我必须编写自己的切换功能,这不是一个大问题,但我宁愿不使用cookie......人们仍然偏执

When I change content of a site using toggle, or which ever other function, the state is not saved. So, if this is my page:

<div id='test'></div>
<input type='submit' value='test'>
<a href='http://www.stackoverflow.com'>link to elsewhere</a>​

and this is in my ready-code:

$(':submit').click( function() {
  $('#test').text( 'this is a test' );
});

when I click the submit button the div will be filled with 'this is a test'. When I follow the link to stackoverflow and click 'back' the DIV will be empty again. How can I make the DIV persistent when clicking to another page, much like in the way <input> elements are persistent (ie. if I enter text in a <input type='text'/> and follow the link that text will still be there when I click 'back')

Eventually I want to save the state of a toggle... or should I use cookies to solve this one, which means I have to write my own toggle functionality, which is not a big problem, but I'd rather not use cookies... people are still paranoid

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酉 2024-10-03 20:25:15

是的,您可以使用 cookies、url 哈希,或者您可以将状态作为字符串存储在“window.name”中。当然,无论您将状态存储在哪里,您都需要在加载时获取它并将状态重置到原来的位置。

window.name 的一个好处是,您可以将整个 HTML 页面存储在其中,然后将它们重新加载回 DOM(包括之前的状态)。当然,其他页面可能会覆盖“window.name”,因此发生这种情况时您需要一个备用计划。

“url hash”非常有用,也可以与 AJAX 一起使用。

了解详情:http://code.google.com/web/ajaxcrawling/docs/规范.html

Yes, you can use cookies,a url hash ,or you can stash the state as a string in "window.name". Of course, regardless of where you store your state, you need to fetch it on load and reset the state to where it was.

One nice thing about window.name if you can stash entire pages of HTML in there, then reload them back into the DOM, previous state included. Of course other pages may be overwriting "window.name" so you need a plan B when this happens.

The "url hash" is pretty useful and can be used with AJAX as well.

Read more: http://code.google.com/web/ajaxcrawling/docs/specification.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文