Get Hash URL from anchor and load into Div

廉价感情. 提交于 2020-01-04 05:34:08

问题


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.

-- The problem --

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

eg.

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.

Help much appreciated.

Thanks


回答1:


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

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:


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.



来源:https://stackoverflow.com/questions/3953299/get-hash-url-from-anchor-and-load-into-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!