Jquery實現顯示和隱藏的4種簡單方式_jquery

來源:互聯網
上載者:User
Html代碼:
複製代碼 代碼如下:

<div class="topicList">
<h3><span>學習天地</span></h3>
<ul>
<li>1111111111</li>
<li>2222222222</li>
<li>333333333</li>
<li>4444444444</li>
<li>5555555555</li>
<li>6666666666</li>
</ul>
</div>

Jquery代碼:
第一種實現方式:
複製代碼 代碼如下:

1. <script type="text/javascript">
$(function(){
$(".topicList h3").click(function(){
var UL = $(this).next("ul");
if(UL.css("display")=="none"){
UL.css("display","block");
}
else{
UL.css("display","none");
}
});
});
</script>

第二種實現方式:
複製代碼 代碼如下:

2. <script type="text/javascript">
$(function(){
$(".topicList h3").toggle(function(){
$(this).next("ul").hide(1000);
},function(){
$(this).next("ul").show(1000);
});
});
</script>

第三種實現方式:
可以使用Jquery提供的show和hide來完成帶緩動的顯示和隱藏效果,由於兩個方法相似,可以直接使用toggle來完成。
複製代碼 代碼如下:

3. <script type="text/javascript">
$(function(){
$(".topicList h3").toggle(function(){
$(this).next("ul").css("display","none");
},function(){
$(this).next("ul").css("display","block");
});
});
</script>

第四種實現方式:
toggle如果有兩個參數,並且都是函數,表示第一次點擊執行第一個函數,第二次點擊執行第二個函數。
複製代碼 代碼如下:

4. <script type="text/javascript">
$(function(){
$(".topicList h3").toggle(topicHandler,topicHandler);
function topicHandler(){
//使用fadeIn、show、slideDown可以完成某個容器的顯示
//使用fadeOut、hide、slideUp可以完成某個容器的隱藏
//所以可以通過各個的toggle來完成兩個之間的輪換
$(this).next("ul").toggle(1000);
}
});
</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.