0

我正在尝试使双层导航正常工作。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');
        }
    });
});​
4

1 回答 1

2

没有$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');
    }
});​
于 2012-12-19T09:39:35.730 回答