Home  >  Q&A  >  body text

javascript - Click on one li to display the content of the div inside, click on the next li to display the content of the div inside the next li, and hide the content displayed on the previous li. How to write js code?

<ul>
<li >热销榜
            <p class="content_item">
                <ul >
                    <li ng-repeat="item in chaoshi">
                        <p><img src="{{item.img}}" alt=""></p>
                        <p>{{item.name}}</p>
                        <p>&nbsp;</p>
                        <p>
                            <p style="color: #b9b9b9;">{{item.specifics}}</p>
                            <p style="color: #ff3800;">¥{{item.price}}</p>
                        </p>
                        <p>+</p>
                    </li>

                </ul>
            </p>
        </li>
        <li >牛奶面包
            <p class="content_item">
                <ul>
                    <li ng-repeat="item in chaoshi1">
                        <p><img src="{{item.img}}" alt=""></p>
                        <p>{{item.name}}</p>
                        <p>&nbsp;</p>
                        <p>
                            <p style="color: #b9b9b9;">{{item.specifics}}</p>
                            <p style="color: #ff3800;">¥{{item.price}}</p>
                        </p>
                        <p>+</p>
                    </li>
                </ul>
            </p>
        </li>
        <li>休闲零食
            <p class="content_item">
                <ul>
                    <li ng-repeat="item in chaoshi2">
                        <p><img src="{{item.img}}" alt=""></p>
                        <p>{{item.name}}</p>
                        <p>&nbsp;</p>
                        <p>
                            <p style="color: #b9b9b9;">{{item.specifics}}</p>
                            <p style="color: #ff3800;">¥{{item.price}}</p>
                        </p>
                        <p>+</p>
                    </li>
                </ul>
            </p>
        </li>
        </ul>
        
怪我咯怪我咯2662 days ago1077

reply all(1)I'll reply

  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-07-05 10:44:55

    I don’t know if the questioner uses JQueryNo, use the siblings() method of JQuery to select sibling elements.

    demo

    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/jquery-3.2.1.min.js">
        </script>
        <style media="screen">
            ul {
                text-align: center;
                color: #FFFFFF;
            }
    
            ul li {
                float: left;
                margin: 20px;
                height: 300px;
                width: 200px;
                list-style: none;
                background: #92aeaf;
                border: 1px solid #CCCCCC;
            }
    
            ul li p {
                width: 100px;
                height: 30px;
                margin: 200px 50px 0 50px;
                background: #ff5d75;
                border: 1px solid #CCCCCC;
                display: none;
            }
        </style>
    </head>
    
    <body>
        <ul>
            <li>我是li,请点击我
                <p class="">
                    我是p
                </p>
            </li>
            <li>我是li,请点击我
                <p class="">
                    我是p
                </p>
            </li>
            <li>我是li,请点击我
                <p class="">
                    我是p
                </p>
            </li>
    
        </ul>
    
    </body>
    <script type="text/javascript">
        $(function() {
            $("li").click(function() {
                $(this).children("p").show();
                $(this).siblings().children("p").hide();
            })
        })
    </script>
    
    </html>
    

    reply
    0
  • Cancelreply