简体   繁体   English

使用jQuery单击父级时,如何使子级元素切换?

[英]How do I make a child element toggle when the parent is clicked using jQuery?

I have three pictures on a page. 我一页上有三张照片。 Each picture has a list of three events underneath them. 每张图片下面都有三个事件的列表。 When the user clicks an event, the info for that event should display under that event. 当用户单击事件时,该事件的信息应显示在该事件下。 Here is my HTML: 这是我的HTML:

<div id="pics">

        <div class="race_box">
            <img src="images/run1.jpg" /><br />
            <figcaption>5k/10k Events</figcaption>

            <div class="races" id="5k">
                <h3>5k/10 Events</h3>
                <ul>
                    <li id="sprint">Mini Sprint</li>
                        <ul>
                            <li class="info">10/30/17<br/>Memorial Park<br/>Appleton</li>
                        </ul>
                    <li id="iron">Iron Horse</li>
                        <ul>
                            <li class="info">11/06/17<br/>Bay Beach Park<br/>Green Bay</li>
                        </ul>
                    <li id="twilight">Twilight Trail</li>
                        <ul>
                            <li class="info">11/13/17<br/>River's Edge Park<br/>Wrightstown</li>
                        </ul>
                </ul>
            </div><!--  End of '5k' div-->
        </div> <!-- End of 'run1' div-->

And here is my CSS: 这是我的CSS:

#header {
width: 100%;
height: 50px;
color: #0000ff;  /*blue*/
padding-top: 25px;
padding-left: 10%
}

h1 {
margin-top: -10px;
}

#main {
position: absolute;
z-index: -999;
display: inline-block;
background-color: #808080;  /*grey*/
width: 100%;
height: 250px;
padding: 1%;
width: 100%;
}

#pics {
width: 66%;
margin: 0 auto;
}

#pics figcaption {
color: #fff;  /*white*/
}

.race_box {
float: left;
width: 215px;
margin-right: 7.3%;
margin-top: 25px;
}
li {
list-style-type: none;
padding: 5px;
color: blue;
display: block;
margin-left: -40px;
}
.info {
display: none;
color: black;
}

I tried using this with the .children.children method, but it would not work. 我尝试将其与.children.children方法一起使用,但无法正常工作。 Here is my JS: 这是我的JS:

$(document).ready(function() {

    $("li").click(function(){
       $(this).children().children().toggle(); 
    });
});

Any suggestions? 有什么建议么? I would like the user to be able to click an event, show the info, click it again and hide it. 我希望用户能够单击事件,显示信息,再次单击它并隐藏它。 Any help is much appreciated! 任何帮助深表感谢!

please wrap the ul contents inside the li tag, then change the jquery like so. 请将ul内容包装在li标记内,然后像这样更改jquery。

 $(document).ready(function() { $("li").on("click", function() { $(this).find('li').toggle('info'); }); }); 
 #header { width: 100%; height: 50px; color: #0000ff; /*blue*/ padding-top: 25px; padding-left: 10% } h1 { margin-top: -10px; } #main { position: absolute; z-index: -999; display: inline-block; background-color: #808080; /*grey*/ width: 100%; height: 250px; padding: 1%; width: 100%; } #pics { width: 66%; margin: 0 auto; } #pics figcaption { color: #fff; /*white*/ } .race_box { float: left; width: 215px; margin-right: 7.3%; margin-top: 25px; } li { list-style-type: none; padding: 5px; color: blue; display: block; margin-left: -40px; } .info { display: none; color: black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="pics"> <div class="race_box"> <img src="images/run1.jpg" /> <br /> <figcaption>5k/10k Events</figcaption> <div class="races" id="5k"> <h3>5k/10 Events</h3> <ul> <li id="sprint">Mini Sprint <ul> <li class="info">10/30/17 <br/>Memorial Park <br/>Appleton</li> </ul> </li> <li id="iron">Iron Horse <ul> <li class="info">11/06/17 <br/>Bay Beach Park <br/>Green Bay</li> </ul> </li> <li id="twilight">Twilight Trail <ul> <li class="info">11/13/17 <br/>River's Edge Park <br/>Wrightstown</li> </ul> </li> </ul> </div> <!-- End of '5k' div--> </div> <!-- End of 'run1' div--> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在jQuery父级外部单击“ this”时,如何切换子级元素? - How do I toggle a child element when clicking “this” from outside of the parent in jQuery? 当单击孩子时,如何使用jQuery将类添加到父元素? - How do I use jQuery to add a class to a parent element when a child is clicked? 单击父级时,如何在jQuery手风琴菜单中同时打开父级的子级和子级? - How do I simultaneously open child and sub-child of a parent, in jQuery accordion menu, when parent is clicked? 在JQuery中单击父元素时如何定位每个子元素 - How to target each child element when their parent is clicked in JQuery 使用jQuery单击元素时如何查找父表单元素? - How to find parent form element when element is clicked using jQuery? 如何使用jquery切换所有子元素的readonly属性 - How do I toggle the readonly attribute of all child element with jquery 单击活生子时,如何禁用父级的onclick? - How do I disable the onclick of parent when live child is clicked? 单击孩子时如何删除父母 - How do I remove a parent when a child is clicked 使用 jQuery 单击时,如何更改一个按钮以切换当前/未来事件? - How do I change one button to toggle current/future events when clicked using jQuery? jQuery:单击父元素时,隐藏子元素。 单击子元素时,不要隐藏它 - jQuery: When parent element is clicked, hide child element. When child element is clicked, don't hide it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM