標籤: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 隱藏/顯示