使 jQuery 可加入书签(即 - 对于内容滑块)
我制作了自己的自定义选项卡式内容脚本,效果很好。唯一缺少的是能够为不同的部分添加书签。
我知道 URL 需要以某种方式重写。目前我正在使用 PreventDefault 来停止页面刷新,这也会阻止 URL 更改。
我也尝试过手动重写 URL,但没有任何反应,因为我猜它需要某种形式的挂钩来检测输入的 URL。
预先感谢,亨利。
编辑:Javascript: http://pastebin.com/1yhzxkUi HTML:http://pastebin.com/WH1CbRZJ
I have made my own custom tabbed content script and it works great. The only thing missing is being able to book mark different sections.
I know the URL needs re-writing somehow. At the moment I am using preventDefault to stop the page refreshing, this also stops the URL from changing.
I have also tried manually re-writing the URL but nothing happens as I guess it needs some form of hooks to detect the entered URL.
Thanks in advance, Henry.
EDIT: Javascript: http://pastebin.com/1yhzxkUi
HTML: http://pastebin.com/WH1CbRZJ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要存储页面的历史记录,最流行且功能齐全/支持的方式是使用 hashchanges。这意味着,假设您从
yoursite/page.html#page1
转到yoursite/page.html#page2
,您可以跟踪该更改,并且因为我们使用哈希值,所以可以跟踪该更改可以通过书签以及后退和前进按钮来拾取。您可以使用 jQuery History 项目找到一种绑定哈希更改的好方法
http://www.balupton.com/projects/jquery-history
还有一个全功能的 AJAX 扩展,允许您轻松地将 Ajax 请求集成到您的状态/哈希中,将您的网站转变为全功能的 Web 2.0 应用程序:
http://www.balupton.com/projects/jquery-ajaxy
他们都提供他们的演示页面上有很棒的文档来解释正在发生的事情和正在发生的事情。
以下是使用 jQuery History 的示例(取自演示站点):
以及 jQuery Ajaxy 的示例(取自演示站点):
如果您想获取查询字符串参数(因此
yoursite/page .html#page1?ab=1&ac=2
)您可以使用:所以请查看这些演示链接以查看它们的实际情况,以及所有安装和使用详细信息。
编辑:查看代码后,这就是将其与 jQuery History 一起使用所需要做的全部事情。
更改:
为:
或者,如果您也计划在其他区域使用 jQuery History,那么我们希望确保只为我们的选项卡调用 switchTab,而不是所有哈希:
我们不再使用 onclick 事件,而是绑定到 jQuery History因为这将检测哈希变化。这是需要理解的最重要的概念,例如,如果我们为该网站添加书签然后返回到该网站,则我们从未单击过它。因此,我们更改点击次数以绑定到 hashchange。当我们单击它、为其添加书签、向后或向前时,hashchange 将始终触发:-)
To store the history of a page, the most popular and full featured/supported way is using hashchanges. This means that say you go from
yoursite/page.html#page1
toyoursite/page.html#page2
you can track that change, and because we are using hashes it can be picked up by bookmarks and back and forward buttons.You can find a great way to bind to hash changes using the jQuery History project
http://www.balupton.com/projects/jquery-history
There is also a full featured AJAX extension for it, allowing you to easily integrate Ajax requests to your states/hashes to transform your website into a full featured Web 2.0 Application:
http://www.balupton.com/projects/jquery-ajaxy
They both provide great documentation on their demo pages to explain what is happening and what is going on.
Here is an example of using jQuery History (as taken from the demo site):
And an example of jQuery Ajaxy (as taken from the demo site):
And if you ever want to get the querystring params (so
yoursite/page.html#page1?a.b=1&a.c=2
) you can just use:So check out those demo links to see them in action, and for all installation and usage details.
Edit: After seeing your code, this is all you would have to do to use it with jQuery History.
Change:
To:
Or if you plan to use jQuery History for other areas too, then we would want to ensure that we only call switchTab for our tabs and not all hashes:
We no longer use a onclick event, instead we bind to jQuery History as that will detect the hashchange. This is the most important concept to understand, as for instance if we bookmark the site then go back to it, we never clicked it. So instead we change our clicks to bind to the hashchange. As when we click it, bookmark it, back or forward, the hashchange will always fire :-)
如果您正在谈论更改 URL 以适应页面上的 AJAX 操作,那么我现在正在做类似的事情。
看看 http://www.asual.com/jquery/address/
这是一个 jQuery 插件,可用于在更改选项卡等时保持地址导航按钮正常工作(或者,您可以只更改 URL 而不会影响历史记录)。
当 URL 在外部(即有人粘贴地址)或内部发生更改时,它会挂钩一些事件。然后您可以从参数中获取值并进行相应更新。
一个简单的使用示例:
If you're talking about changing the URL to suit AJAX operations on the page, then I'm doing a similar thing at the moment.
Have a look at http://www.asual.com/jquery/address/
It's a plugin for jQuery and is useful to keep the address navigation buttons working when you change tabs, etc (or, you can just change the URL without affecting the history).
It has events to hook into for when the URL changes externally (i.e. someone pasting the address) or internally. Then you can pick up the values from the parameters and update accordingly.
A simple usage example:
使用哈希链接允许可添加书签/可共享的链接触发 JavaScript 代码,而不是重新加载页面。 Ben Almans jQuery hashchange 事件 允许您将事件处理程序绑定到 hashchange 事件,该插件适用于通常不支持此功能的旧版浏览器。绑定到 hashchange 的事件处理程序可以读取 url 的哈希部分,并调用任何函数。
该另一个事件处理程序可以处理同一 URL 中由点 ('.') 分隔的 2 个参数。
如果使用正则表达式,则可以解析任意字符组合。
这将转换 url:
进入以下 JavaScript 对象:
那么只需在所选元素上运行 jQuery 的 show() 或 hide() 函数即可。
这可以转换为非 jQuery JavaScript,但随后您将失去 Ben Alman 提供的功能,这对于便携式解决方案至关重要。
Using hash links allows for bookmarkable/sharable links to trigger JavaScript code, instead of reloading the page. Ben Almans jQuery hashchange event allows you to bind an event handler to the hashchange event, this plugin works with older browsers that don't support this normally. An event handler bound to hashchange can read the hash part of the url, and call any function.
This other event handler can process 2 arguments separated by a dot ('.') in the same url.
If regular expressions are used, any combination of characters can be parsed.
This will transform the url:
Into the following JavaScript Object:
Then it is only a matter of running jQuery's show() or hide() functions on your selected elements.
This could be transformed into non-jQuery JavaScript, but then you would lose the functionality that Ben Alman delivers, which is crucial for a portable solution.
您可以随时检查此插件: jQuery BBQ (返回按钮和查询)来添加要添加书签的 #hash,就像 Facebook 所做的那样。
You can always check this plugin: jQuery BBQ (Back Button & query) to add a #hash to be bookmarked, like Facebook do.