开发者

get class name from group of links in ul

开发者 https://www.devze.com 2023-02-07 13:06 出处:网络
hey guys, i\'ve been pulling my hair out over this one. I need to get the class name of each link in an unordered list. I know it sounds easy enough, but it\'s a little more complicated than

hey guys, i've been pulling my hair out over this one. I need to get the class name of each link in an unordered list. I know it sounds easy enough, but it's a little more complicated than

$('ul > li > a').attr('class');

I have a pretty large menu of all the departments in our company. there are something like 33-34 items in the list. Each department except 3 or so has a sub department, and many of the items in each sub department have a sub department. Now I'm the only programmer here and my bosses wanted a solution where a non-programmer could add/remove items from the menu. Due to limitations with the ecommerece platform we use (easystore creator, and no i wouldn't recommend it to anyone) I can't use a database or any server side languages. So I'm using a XML file that contains all the departments, sub departments and level three departments. I then use the $.ajax() method to load the xml file and parse the XML data into the menu.

I'm good until I get to the part where I have to build the level three menus. I have an array that contains true/false as to if the given submenu has a level three menu in it somewhere. The position of the bool in the array plus 1 corresponds with the submenu. So if flag[0] = false it means that submenu1 has no level three menu.

I went ahead and appended an unordered list with the class level3 to each item in the submenu if it has a level three menu in it somewhere. In the XML file the node that contains the level three information is called <levelThreeItem> and has an attribute for that tells which item in the submenu the level three menu goes to. The for attribute is formatted deptXX.YY where XX is the submenu it belongs to and YY is the item in the submenu it belongs to. So if it where dept05.02 it means that submenu 05, item 02 (items start counting at 00).

This is wear the class comes in. Each link in the first sub menu, or levelTwo as I call it, has a class levelTwoLinkXX where XX is the order of the links. Ex. if there are 4 links in the submenu the first link is levelTwoLink00 and the last is leveTwoLink03. Now what I am trying to do is fill an array with the classes of the levelTwo menu only if it has a level three menu. So far the only thing i've managed to accomplish i开发者_Python百科s load all the classes for each link from every levelTwo menu into the array.

I hope I've explained everything. If there is anything I need to clear up, please let me know; and thanks in advance

Edit: html structure:

<div id="leftNav">
    <ul class="level1">
        <li><a href="#">Dept 1</a>
            <ul class="level2">
                <li class="deptName"><a href="#">Dept 1</a></li>
                <li><a href="#" class="levelTwoLink00">Dept1.1</a>
                    <ul class="level3"></ul>
                </li>
                <li><a href="#" class="levelTwoLink01">Dept1.2</a>
                    <ul class="level3"></ul>
                </li>
            </ul>
        </li>
    </ul>
</div>

Suppose levelTwoLink01 has a level three menu and levelTwoLink00 does not. I need to remove the level3 ul from that list item


Maybe something like this...

var level2sWithLevel3s = [];
$('.level3').each(function() {
    level2sWithLevel3s.push($(this).prev().attr('class'));
});
0

精彩评论

暂无评论...
验证码 换一张
取 消