从锚点获取哈希值并加载到Div中

时间:2022-11-27 17:55:34

I have a video gallery that has a menu on the left and loads the content into a div on the right hand side. The menu is generated from php video posts so we need a general script that will effect everything.

我有一个视频库,左侧有一个菜单,并将内容加载到右侧的div中。菜单是从php视频帖子生成的,因此我们需要一个能够影响所有内容的通用脚本。

-- The problem --

- 问题 -

The links will load the URL of the video as an anchor on the current URL -

链接将加载视频的URL作为当前URL的锚点 -

eg.

例如。

http://www.divethegap.com/update/community/videos/#http://www.divethegap.com/update/2010/10/test-video-2/

http://www.divethegap.com/update/community/videos/#http://www.divethegap.com/update/2010/10/test-video-2/

So all I need is a script that will get the hash tag and load the content into a div. So far I have failed miserably trying to do that. I imagine it is something along the lines of document.location.hash but don't know where to go from there.

所以我需要的是一个脚本,它将获取哈希标记并将内容加载到div中。到目前为止,我已经失败了,试图做到这一点。我想这是document.location.hash的内容,但不知道从那里去哪里。

Help much appreciated.

非常感谢。

Thanks

谢谢

2 个解决方案

#1


4  

Note: this answer users jQuery because 1.4.2 is included and being used extensively in the page already.

注意:这个答案用户jQuery,因为1.4.2已经包含在页面中并且已经广泛使用了。

You can attach a click handler to the anchors, for example:

您可以将单击处理程序附加到锚点,例如:

$("#nav a").click(function() {
  $("#content").load(this.hash.substring(1));
});

You can test it out against your markup here, not it won't actually load in the demo since it's on a separate domain, but will work fine on the same domain.

您可以在这里针对您的标记测试它,而不是它实际上不会加载到演示中,因为它在一个单独的域上,但在同一个域上可以正常工作。

#2


1  

You can try this -

你可以尝试这个 -

$('a').click(function(){

  $('#content').load(document.location.hash.replace(/#/,''));

});

This will load the content after hash part from the current url.

这将从当前url加载哈希部分之后的内容。

#1


4  

Note: this answer users jQuery because 1.4.2 is included and being used extensively in the page already.

注意:这个答案用户jQuery,因为1.4.2已经包含在页面中并且已经广泛使用了。

You can attach a click handler to the anchors, for example:

您可以将单击处理程序附加到锚点,例如:

$("#nav a").click(function() {
  $("#content").load(this.hash.substring(1));
});

You can test it out against your markup here, not it won't actually load in the demo since it's on a separate domain, but will work fine on the same domain.

您可以在这里针对您的标记测试它,而不是它实际上不会加载到演示中,因为它在一个单独的域上,但在同一个域上可以正常工作。

#2


1  

You can try this -

你可以尝试这个 -

$('a').click(function(){

  $('#content').load(document.location.hash.replace(/#/,''));

});

This will load the content after hash part from the current url.

这将从当前url加载哈希部分之后的内容。