0

I have an unordered list with the following structure.

If I click the link in List Item 1, I want to be able to select all of the links (anchor tags) underneath List Item 1 and change the class to be "yes".

How?

    <ul>
        <li><div><span><a class="no" href="#">List Item 1</a></span></div>
            <ul>
                <li><div><span><a class="no" href="#">Sub List Item 1</a></span></div></li>
                <li><div><span><a class="no" href="#">Sub List Item 2</a></span></div>
                    <ul>
                        <li><div><span><a class="no" href="#">Sub Sub List Item 1</a></span></div></li>
                        <li><div><span><a class="no" href="#">Sub Sub List Item 2</a></span></div></li>
                        <li><div><span><a class="no" href="#">Sub Sub List Item 3</a></span></div></li>
                    </ul>
                </li>
                <li><div><span><a class="no" href="#">Sub List Item 3</a></span></div></li>
            </ul>
        </li>
        <li><div><span><a class="no" href="#">List Item 2</a></span></div>
            <ul>
                <li><div><span><a class="no" href="#">Sub List Item 5</a></span></div></li>
                <li><div><span><a class="no" href="#">Sub List Item 6</a></span></div></li>
                <li><div><span><a class="no" href="#">Sub List Item 7</a></span></div></li>
            </ul>
        </li>
    </ul>

Answer

        $(href).closest('li').find('a').each(function (index, child) {
            $(child).removeClass('no').addClass('yes');
            //Do more logic here
        });
4

1 に答える 1

1
$('.no').on('click', function () {
    $(this).closest('li').find('a').andSelf().addClass('yes').removeClass('no');
});
于 2012-10-17T21:39:30.260 に答える