我正在尝试让双层导航工作。一切都很酷,但是根据URL将类current
设置为正确的A元素的jQuery。但即使是警报也不起作用,我也不确定为什么(jQuery 的新手)。
目前:远程测试站点
$(document).ready(function(){
// url
var $path = location.pathname.split("/");
var $topnav_path = path[1];
var $filternav_path = path[2];
// current page
var $test = "the category is: " + $topnav_path + " AND the filter is: " + $filternav_path;
alert($test);
// navigation
$(function() {
// set link to current page
if ( $path[1] ) {
$('#top_nav a[class$="' + $topnav_path + '"]').toggleClass('current');
}
// if link is root, set first child (home)
if ( !$path[1] ) {
$('#top_nav a:first').toggleClass('current');
}
// set filter to current filter
if ( $path[2] ) {
$('#filter_nav a[class$="' + $filternav_path + '"]').toggleClass('current');
}
// if link is root, set first child (home)
if ( !$path[2] ) {
$('#filter_nav a:first').toggleClass('current');
}
});
});
根据下面的第一个答案,以下内容也不起作用(无论是否使用函数调用):
$(document).ready(function(){
// url
var path = location.pathname.split("/"),
topnav_path = path[1] != undefined ? path[1] : false,
filternav_path = path[2] != undefined ? path[2] : false;
// test url
var test = "the category is: " + topnav_path + " AND the filter is: " + filternav_path;
alert(test);
// navigation
$(function() {
// set top nav to current category or home
if ( path[1] ) {
$('#top_nav a[class$="' + topnav_path + '"]').toggleClass('current');
}else{
$('#top_nav a:first').toggleClass('current');
}
// set filter nav to current filter or all
if ( path[2] ) {
$('#filter_nav a[class$="' + filternav_path + '"]').toggleClass('current');
}else{
$('#filter_nav a:first').toggleClass('current');
}
});
});
没有$test
变量,你把它命名为 test
。其他变量也是如此。
$(document).ready(function(){
var path = location.pathname.split("/"),
topnav_path = path[1] != undefined ? path[1] : false,
filternav_path = path[2] != undefined ? path[2] : false;
var test = "the category is: " + topnav_path + " AND the filter is: " + filternav_path;
if ( path[1] ) {
$('#top_nav a[class$="' + topnav_path + '"]').toggleClass('current');
}else{
$('#top_nav a:first').toggleClass('current');
}
if ( path[2] ) {
$('#filter_nav a[class$="' + filternav_path + '"]').toggleClass('current');
}else{
$('#filter_nav a:first').toggleClass('current');
}
});