我正在尝试使双层导航正常工作。current
一切都很酷,但是 jQuery根据 URL将类设置为正确的 A 元素。但甚至警报都不起作用,我不确定为什么(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');
}
});
});