1. 程式人生 > >jQuery遍歷多個相同的class,點選顯示隱藏圖示,顯示及隱藏其下的內容

jQuery遍歷多個相同的class,點選顯示隱藏圖示,顯示及隱藏其下的內容


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>jQuery便利多個相同的class,點選顯示隱藏圖示,顯示及隱藏其下的內容</title
>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script> </head> <body> <style>
body{ background-color: #FFFFFF; letter-spacing: 2px; } .box{ padding-top: 100px; width: 50%; margin: 0 auto; } ul li ul{ margin: 20px 0 20px 40px; } .img_sh{ margin: 20px 0; } .img_sh img{ float: left; padding-right
: 10px
; }
</style> <script> //頁面載入完畢後加載 window.onload = function () { //遍歷class類名叫img_sh的li $(".img_sh").each(function () { //將遍歷到的類賦給li var li = $(this); //執行操作,li的子元素p標籤裡的子元素類名為jia的加號圖片點選後執行函式 li.children("p").children(".jia").click(function () { //點選的自身加號圖片要隱藏 li.children("p").children(".jia").hide(500); //點選完的加號圖片要隱藏,而相反類名為jian的減號圖片要顯示,toggle的作用是取反(既顯示或隱藏) li.children("p").children(".jian").toggle(500); //下面的ul列也是取反 li.children("ul").toggle(500); }) //同理 li.children("p").children(".jian").click(function () { li.children("p").children(".jian").hide(500); li.children("p").children(".jia").toggle(500); li.children("ul").toggle(500); }) }) } </script> <div class="container-fluid"> <div class="box "> <ul class="list-unstyled"> <li class="img_sh"> <p> jq測試 <img src="images/img01.jpg" alt="" class="img-responsive jia" style="display: none"> <img src="images/img02.jpg" alt="" class="img-responsive jian" style="display: block" > </p> <ul class="list-unstyled" style="display: block"> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> </ul > </li> <li class="img_sh"> <p> jq測試 <img src="images/img01.jpg" alt="" class="img-responsive jia" style="display: block" > <img src="images/img02.jpg" alt="" class="img-responsive jian" style="display: none"> </p> <ul class="list-unstyled" style="display: none"> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> </ul > </li> <li class="img_sh"> <p> jq測試 <img src="images/img01.jpg" alt="" class="img-responsive jia" style="display: block" > <img src="images/img02.jpg" alt="" class="img-responsive jian" style="display: none"> </p> <ul class="list-unstyled" style="display: none"> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> </ul > </li> <li class="img_sh"> <p> jq測試 <img src="images/img01.jpg" alt="" class="img-responsive jia" style="display: block" > <img src="images/img02.jpg" alt="" class="img-responsive jian" style="display: none"> </p> <ul class="list-unstyled" style="display: none"> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> </ul > </li> <li class="img_sh"> <p> jq測試 <img src="images/img01.jpg" alt="" class="img-responsive jia" style="display: block" > <img src="images/img02.jpg" alt="" class="img-responsive jian" style="display: none"> </p> <ul class="list-unstyled" style="display: none"> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> <li>●&nbsp 吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮;</li> </ul > </li> </ul> </div> </div> </body> </html>

方法二:


        $(".iconfontBtn").each(function () {
            var iconfontBtn = $(this);
                iconfontBtn.click(function(){
                    // alert('qwe');
                    var isHidden = $('.iconfontHidden').css('display')
                    if (isHidden == 'block') {
                        // $('#iconfontHidden').css('display','none');
                        // $(".iconfontBtn").next('div').hide(500);
                        // iconfontBtn.next('div').hide(500);
                        iconfontBtn.next('div').slideToggle(500);
                        // $(".iconfontHidden").closest('div').hide(500);
                    }else if(isHidden == 'none'){
                        // iconfontBtn.next('div').toggle(500);
                        iconfontBtn.next('div').slideToggle(500);
                        // $(".iconfontBtn").next('div').toggle(500);
                        // $(".iconfontHidden").closest('div').toggle(500);
                        // $('#iconfontHidden').css('display','block');
                    }
                })
        })