我如何使用jQuery创建可书签的哈希? [英] How do I use jQuery to create a bookmark-able hash?

查看:138
本文介绍了我如何使用jQuery创建可书签的哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网址用作图片库。在这个网址是一个大拇指 div (为简洁起见我只展示了四个大拇指):

I have a single URL I am using as an image gallery. At this URL is a thumbs div (I'm only showing four thumbs for the sake of brevity):

 <div id="thumbs">
 <img src="graphics/thumbs/01.jpg" width="190" height="190" class="thumb objects" id="project01" />
 <img src="graphics/thumbs/08.jpg" width="190" height="190" class="thumb web" id="project08" />
 <img src="graphics/thumbs/14.jpg" width="190" height="190" class="thumb freehand" id="project14"/>
 <img src="graphics/thumbs/04.jpg" width="190" height="190" class="thumb freehand objects" id="project04" /></div>

空的 div 称为<$ c

$ p $ lt; code>< div id =content>< / / DIV>

和一个隐藏的 div 称为 #preload

and a hidden div called #preload:

我的jQuery使用被点击的缩略图的ID到 clone() code>来自相应 span 的图片,并使用将它们放入 #content > html()方法。它效果很好。现在,我想返回到 mysite.com/#project01 ,并且已经使用Project 01填充了 #content

我怎样才能给每个图像一个哈希的URL来调用相同的jQuery状态?

<div id="preload"> <span id="project01_content"><img src="graphics/010.jpg" /></span> <span id="project02_content"><img src="graphics/022.jpg" /><img src="graphics/021.jpg" /><img src="graphics/023.jpg" /><img src="graphics/020.jpg" /></span> <span id="project03_content"><img src="graphics/030.jpg" width="450" height="600" /><img src="graphics/031.jpg" width="450" height="600" /></span> <span id="project04_content"><img src="graphics/040.jpg" width="775" height="600" /><img src="graphics/041.jpg" width="775" height="600" /></span></div>

推荐答案

推荐答案

(window.location.hash.indexOf('project')> -1)
$(window.location.hash)中的函数(){
。点击();
});

$(function() { if(window.location.hash.indexOf('project') > -1) $(window.location.hash).click(); });

这使用 window.location.hash 属性这将是#project01在你的示例URL中...所以它已经是一个 #ID 选择器,我们只是检查它是我们关心的散列,然后使用该选择器来触发 .click() 。只要确保这个命令在你的当前点击处理器被绑定之后运行。

This uses the window.location.hash property which would be "#project01" in your example URL...so it's already an #ID selector, we're just checking that it's a hash we care about, then using that selector to fire a .click(). Just make sure this runs after your current click handler for the thumbs is bound.

这篇关于我如何使用jQuery创建可书签的哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆