CSS所以,我的问题是,我有“选择”,告诉你什么你菜单上。我使用两个类,selected
和unselected
类。添加/删除页面加载
页面加载时,我加载基于URL的哈希值,并加载罚款(这意味着我的功能,当网页加载,作品!)
现在,我有一个单独的方法,叫做setSelect()
基本上基于在我的JS文档的开始处声明的变量,将CSS添加到菜单项中。
因此,这里的代码(我砍掉了很多了,只保留相关部分)
var jlLocation = 1;
function setSelect() {
if(jlLocation==1) {
$('#location1').removeClass('unselected');
$('#location1').addClass('selected');
$('#location2').removeClass('selected');
$('#location2').addClass('unselected');
}
else if(jlLocation==2) {
$('#location1').removeClass('selected');
$('#location1').addClass('unselected');
$('#location2').removeClass('unselected');
$('#location2').addClass('selected');
}
}
$(function(){
// Bind an event to window.onhashchange that, when the hash changes, gets the
// hash and adds the class "selected" to any matching nav link.
$(window).hashchange(function(){
hash = location.hash;
document.title = 'Joy Luck ' + (hash.replace(/^#/, '- ') || '') + ''; // Set the page title based on the hash.
setSelect();
if (hash == "#Home") {
showHome();
}
else if (hash == "#Accommodations") {
showAccommodations();
}
$('#menu a').each(function(){
var that = $(this);
that[ that.attr('href') == hash ? 'addClass' : 'removeClass' ]('selected');
});
})
$(window).hashchange();
});
再次,如果我在#home
散列或#accommodations
这些方法运行加载,但setSelect();
方法不起作用。所有方法都放在加载哈希的方法之前,所以这不应该成为问题。
编辑
我知道setSelect()
没有被调用。那是因为无论我用那种方法称呼它,它都不起作用。但是,为了帮帮忙,我把它英寸
它是一个错字代码缺少}上第一个函数关闭它?你真的需要重用选择器。不要多次执行$('#location1')。链接他们! $( “XXX”)addClass( “X”)removeClass( “Y”)。; – epascarello 2011-05-07 21:36:37
对不起,它没有存活我的粘性复制和粘贴方法。它存在于我的原始代码中。 – Johannes 2011-05-07 21:37:35
什么叫setSelect()? – epascarello 2011-05-07 21:41:04