Selector for the child element for the current parent element

Muneeb Pullani

I have a category list. I wish to toggle my each category items when a click is done on the respective category.

I did the below code. But when I click any of the category, all the category items are toggled. I think the problem is with my selector. What change I should do now.

HTML

<div class="nav">
    <ul>
        <li class="main_category">Category1</li>
            <div class="sub_category" style="display: none;">
                <ul>
                    <li>Item1</li>
                    <li>Item2</li>
                </ul>
            </div>
        <li class="main_category">Category2</li>
            <div class="sub_category" style="display: none;">
                <ul>
                    <li>Item1</li>
                    <li>Item2</li>
                </ul>
            </div>
    </ul>
</div> 

jQuery

$(document).ready(function() {
    $("li.main_category").click(function() {
        $(".sub_category").slideToggle();
    });
});
tika

If you do not want to change your HTML markup, (assuming the item will div will remain next to li):

$(document).ready(function() {
    $("li.main_category").click(function() {
        $(this).next().slideToggle();
    });
});

But, I would suggest you to change your markup a little (because you don't need or may not want divs inside the list) so that it is cleaner:

<div class="nav">
        <ul>
            <li class="main_category">Category1
                    <ul style="display:none">
                        <li>Item1</li>
                        <li>Item2</li>
                    </ul>
                </li>
            <li class="main_category">Category2
                    <ul style="display:none">
                        <li>Item1</li>
                        <li>Item2</li>
                    </ul>
                </li>
        </ul>
</div> 

And the script:

$(document).ready(function() {
    $("li.main_category").click(function() {
        $(this).children("ul").slideToggle();
    });
});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

CSS selector - element with a given child

分類Dev

Overflowing a child element outside the parent element

分類Dev

XPath find parent of a child element

分類Dev

Child elements expanding outside parent element with flex

分類Dev

Appending an element to the child fires the drop event in the parent

分類Dev

Angular directive on parent to change class of a child element?

分類Dev

get child array values in a parent div element

分類Dev

Unwrapping element from parent if it is an only child - JSoup

分類Dev

Selecting multiple child elements of a certain parent element

分類Dev

XPath to find dynamic parent/ancestor element from current element?

分類Dev

How can I hide parent element if child element is hidden?

分類Dev

Check if an span element contains a "text" and is the last child of a parent element

分類Dev

How do you disable the set width of parent element for child element?

分類Dev

jquery Checking if parent element has a specific text within it, if so then add child element after specific pre-existing child element

分類Dev

Percentage width child element in absolutely positioned parent on Internet Explorer 7

分類Dev

Traversing back to parent and go to all its child element with ($this)

分類Dev

Vertically align a child div element with parent div having dynamic height

分類Dev

How to find child element with the help of parent's ref?

分類Dev

How to disable rendering of parent element if child attribute is true

分類Dev

How to get the left and top position of a child element of its parent

分類Dev

Child span element getting out of parent element, flexbox / margin - padding issue

分類Dev

How to center an absolutely positioned element within its parent when the child element's height is unknown

分類Dev

Horizontal scroll only if child element's width property exceeds parent element's max-width property

分類Dev

Is there a way to prevent background of a parent element from showing up on the edge when both parent and child have a border radius?

分類Dev

Angular Directive not for child element

分類Dev

selecting an element with css selector python

分類Dev

Extract CSS Selector for an element with Selenium

分類Dev

Jquery syntax selector element issue

分類Dev

Select parent element in stylus

Related 関連記事

  1. 1

    CSS selector - element with a given child

  2. 2

    Overflowing a child element outside the parent element

  3. 3

    XPath find parent of a child element

  4. 4

    Child elements expanding outside parent element with flex

  5. 5

    Appending an element to the child fires the drop event in the parent

  6. 6

    Angular directive on parent to change class of a child element?

  7. 7

    get child array values in a parent div element

  8. 8

    Unwrapping element from parent if it is an only child - JSoup

  9. 9

    Selecting multiple child elements of a certain parent element

  10. 10

    XPath to find dynamic parent/ancestor element from current element?

  11. 11

    How can I hide parent element if child element is hidden?

  12. 12

    Check if an span element contains a "text" and is the last child of a parent element

  13. 13

    How do you disable the set width of parent element for child element?

  14. 14

    jquery Checking if parent element has a specific text within it, if so then add child element after specific pre-existing child element

  15. 15

    Percentage width child element in absolutely positioned parent on Internet Explorer 7

  16. 16

    Traversing back to parent and go to all its child element with ($this)

  17. 17

    Vertically align a child div element with parent div having dynamic height

  18. 18

    How to find child element with the help of parent's ref?

  19. 19

    How to disable rendering of parent element if child attribute is true

  20. 20

    How to get the left and top position of a child element of its parent

  21. 21

    Child span element getting out of parent element, flexbox / margin - padding issue

  22. 22

    How to center an absolutely positioned element within its parent when the child element's height is unknown

  23. 23

    Horizontal scroll only if child element's width property exceeds parent element's max-width property

  24. 24

    Is there a way to prevent background of a parent element from showing up on the edge when both parent and child have a border radius?

  25. 25

    Angular Directive not for child element

  26. 26

    selecting an element with css selector python

  27. 27

    Extract CSS Selector for an element with Selenium

  28. 28

    Jquery syntax selector element issue

  29. 29

    Select parent element in stylus

ホットタグ

アーカイブ