如何分配类并切换可扩展菜单项

时间:2015-09-30 14:36:20

标签: javascript jquery html css

$('.myButton').click(function() {
    var vStr = "2.1";
    var vULSize = $("#uSPStyle li:not(li li)").size();
    var vULSubSize;
    if (vStr.indexOf('.') !== -1) {
        var vSplit = vStr.split(".");
        var vFirst = vSplit[0];
        var vSecond = vSplit[1];
        console.log(vFirst); //first number
        console.log(vSecond); //second number
        console.log(vULSize); //size of the parent UL
        if (vFirst <= vULSize) { //if the first number is less than or equal to the parent UL size
            //{need help}remove 'current' class from the existing parent LI
            $("#uSPStyle li:eq(" + vFirst + ")").addClass("current"); //{need help}add the 'current' class to the parent LI
            vULSubSize = ""; //{need help}get the size of the sub UL inside the above LI
            if (vSecond <= vULSubSize) { //if the number is less than or equal to the size of the sub UL
                $("#uSPStyle li:eq(" + vFirst + ")").find("ul").slideToggle(); //{need help}expand the sub UL inside the LI
                $("#uSPStyle li:eq(" + vFirst + ")").find("ul li:eq(" + vSecond + ")").addClass("current"); //{need help}add the 'current class to the sub UL LI
                $('.dispArtBody').addClass('hideContent'); //{need help}hide all content
                var element = $("#uSPStyle li:eq(" + vFirst + ")").find("ul li:eq(" + vSecond + ")").attr("data-toggle"); //{need help}get the class of the LI which corresponds to the body class
                $(element).removeClass('hideContent'); //{need help}show that content which corresponds to the element
            }
        }
    }
    else {
        //{need help}remove 'current' class from the existing parent LI
        $("#uSPStyle li:eq(" + vFirst + ")").addClass("current"); //{need help}add the 'current' class to the parent LI
        $('.dispArtBody').addClass('hideContent'); //{need help}hide all content
        var element = $("#uSPStyle li:eq(" + vFirst + ")").attr("data-toggle"); //{need help}get the class of the LI which corresponds to the body class
        $(element).removeClass('hideContent'); //{need help}show that content which corresponds to the element
    }
});

小提琴:http://jsfiddle.net/5qweu58f/6/

例如,如果vStr2.1,那么“关注”LI将丢失current类,“BC”LI应该获得current 1}}类。 “我们的链接”LI也应该显示current类,并且应显示<div class="tf1SLink2 dispArtBody hideContent"><tf1SText02>This is for second link sublink 1</tf1SText02></div>,因为它对应于子UL的第一个LI到父UL的第二个LI。

请帮我完成上面的脚本来完成它。 removeClass和addClass对我来说根本不起作用。

我只是想通过页面加载事件来复制click事件。因此2.1将位于网址中,例如:www.mysite1.com/mypage.aspx?id=9090&menuid=2.1

1 个答案:

答案 0 :(得分:1)

只有次要编辑:

if (vFirst <= vULSize) {
    // remove .current from every top level just to be sure
    $("#uSPStyle li a").removeClass("current");

    // next line accomplishes two goals:
    // adds .current to the a tag inside the correct li (you were adding it to the li)
    // gets the size for vULSubSize (you were setting this to "" for some reason)
    vULSubSize = $("#uSPStyle > li:eq(" + (vFirst-1) + ") a").addClass("current").size();

您可以在上面看到,当我们添加li时,我已指定$("#uSPStyle")必须是.current的直接后代,这是因为还有其他较低的杠杆儿童这不应该适用于。我们现在也使用(vFirst-1) - 因为:eq()在索引的基础上工作,字符串中的1等于第0个元素。

更正了fiddle

对于第二级(编辑后),您可能希望使用

$("#uSPStyle > li:eq(" + vFirst + ") > ul > li:eq(" + vSecond + ") a").addClass("current");

http://jsfiddle.net/5qweu58f/9/