jQuery 隱藏/顯示

來源:互聯網
上載者:User

標籤:on()   slow   color   .sh   ack   nts   query   type   切換   

1.如何隱藏部分文本(簡單案例)

//樣式

<style type="text/css">
            div.ex {
                background-color: #e5eecc;
                padding: 7px;
                border: solid 1px #c3c3c3;
                width: 300px;
            }
        </style>

//jq代碼

<script type="text/javascript" src="js/jquery-3.1.1.js"></script>
        <script type="text/javascript">
            
            $(function() {
                $(‘.hide‘).on(‘click‘, function() {//擷取按鈕給它添加點擊事件
                    $(this).parents(‘.ex‘).hide(‘slow‘);//點擊按鈕後它的父元素隱藏  slow:緩慢隱藏,實現動畫效果

        //也可將slow換成數字,如hide(1000):表示隱藏的整個過程用時1000毫秒(即一秒鐘)
                })
            })
        </script>

//html代碼

<body>

        <h3>中國辦事處</h3>
        <div class="ex">
            <button class="hide" type="button">隱藏</button>
            <p>連絡人:張先生<br /> 北三環中路 100 號<br /> 北京
            </p>
        </div>

        <h3>美國辦事處</h3>
        <div class="ex">
            <button class="hide" type="button">隱藏</button>
            <p>連絡人:David<br /> 第五大街 200 號<br /> 紐約
            </p>
        </div>

    </body>

 

 

2.使用 hide() 和 show() 方法來隱藏和顯示 HTML 元素:

//jq代碼

        <script type="text/javascript" src="js/jquery-3.1.1.js"></script>
        <script type="text/javascript">
            $(function(){
                $(‘#hide‘).on(‘click‘,function(){//擷取隱藏按鈕給它添加點擊事件
                    $(‘#p1‘).hide(‘slow‘);//點擊按鈕後p標籤隱藏  slow:緩慢隱藏,實現動畫效果
                });
                $(‘#show‘).on(‘click‘,function(){//擷取顯示按鈕給它添加點擊事件
                    $(‘#p1‘).show(‘slow‘);//點擊按鈕後p標籤顯示  slow:緩慢顯示,實現動畫效果
                })
            })
        </script>

//html代碼

<body>
        <button id="hide" type="button">隱藏</button>
        <button id="show" type="button">顯示</button>
        <p id="p1">如果點擊“隱藏”按鈕,我就會消失。</p>
        
    </body>

3.使用 toggle() 方法來切換 hide() 和 show() 方法。

   //jq代碼   

<script type="text/javascript" src="js/jquery-3.1.1.js"></script>
        <script type="text/javascript">

            $(function(){
                $(‘:button‘).on(‘click‘,function(){
                    $(‘p‘).toggle(1000);//表示點擊的時候如果p標籤顯示就讓它隱藏,反正則顯示
                })
            })
        </script>

//html代碼

<body>
        <button type="button">切換</button>
        <p>這是一個段落。</p>
        <p>這是另一個段落。</p>
    </body>

 

jQuery 隱藏/顯示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.