问题描述
我有以下链接:
<a href="#HDR" onClick="showGallery()">I</a>
这使用以下javascript:
And this use the following javascript:
function showGallery(){
if(window.location.hash) {
$('#gallery').fadeIn('fast');
var hash = window.location.hash.substring(1);
alert(hash);
} else {
}
}
因此,只有在URL中显示标签时才显示图库。但是,当我点击链接时,没有任何反应。当我点击它两次时,画廊淡入。
So it only show the gallery when in the URL is a hashtag. But when i click on the link, nothing happens. When i click it twice, the gallery fade in.
所以链接首先制作javascript,我不能正常工作'因为URL中没有主题标签之后,它执行href并在URL中插入Hashtag。
我该怎么做?
So the link first make the javascript, and i doesn't work 'cause there is no hashtag in the URL and after that, it perform the href and insert the Hashtag in the URL.How can i do that?
我的目标:
当我点击链接时,它开了一个画廊。要知道我必须打开哪个库,我在URL中插入一个标签。在这里,我想显示HDR专辑。我还想要,如果我的网站使用标签获得opend,它应该显示图库。
When i click on a link, it open a gallery. To know which gallery i must open, i insert in the URL a Hashtag. Here i want to display the HDR album. And i also want, if my site get opend with a hashtag, it should display the gallery.!
还有另一种更简单或更清洁的方法吗?
希望你理解我想要的东西。
Is there also a another, easier or cleaner way to make it?Hope you understand what i want.
推荐答案
对于现代浏览器,你可以将你的Javascript代码绑定到事件。链接将不使用Javascript:
For modern browsers, you can bind your Javascript code to the onhashchange event. Links will be without Javascript:
<a href="#HDR">I</a>
每当哈希值发生变化时运行Javascript:
And the Javascript is run whenever the hash has changed:
function locationHashChanged() {
if (location.hash === "#HDR") {
$('#gallery').fadeIn('fast');
}
}
window.onhashchange = locationHashChanged;
这篇关于在onClick之前执行href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!