本節知識點請移步查看:史上最全jQuery知識點小節
文章目錄
- 【案例】新浪下拉選單
- 【案例】tab欄切換案例
- 【案例】微博發布
【案例】新浪下拉選單
實作效果:

代碼:
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style-type: none;
}
a {
text-decoration: none;
font-size: 14px;
}
.nav {
margin: 100px;
}
.nav>li {
position: relative;
float: left;
width: 80px;
height: 41px;
text-align: center;
}
.nav li a {
display: block;
width: 100%;
height: 100%;
line-height: 41px;
color: #333;
}
.nav>li>a:hover {
background-color: #eee;
}
.nav ul {
display: none;
position: absolute;
top: 41px;
left: 0;
width: 100%;
border-left: 1px solid #FECC5B;
border-right: 1px solid #FECC5B;
}
.nav ul li {
border-bottom: 1px solid #FECC5B;
}
.nav ul li a:hover {
background-color: #FFF5DA;
}
</style>
<ul class="nav">
<li>
<a href="#">微博</a>
<ul>
<li>
<a href="#">私信</a>
</li>
<li>
<a href="#">評論</a>
</li>
<li>
<a href="#">@我</a>
</li>
</ul>
</li>
<li>
<a href="#">微博</a>
<ul>
<li>
<a href="#">私信</a>
</li>
<li>
<a href="#">評論</a>
</li>
<li>
<a href="#">@我</a>
</li>
</ul>
</li>
<li>
<a href="#">微博</a>
<ul>
<li>
<a href="#">私信</a>
</li>
<li>
<a href="#">評論</a>
</li>
<li>
<a href="#">@我</a>
</li>
</ul>
</li>
<li>
<a href="#">微博</a>
<ul>
<li>
<a href="#">私信</a>
</li>
<li>
<a href="#">評論</a>
</li>
<li>
<a href="#">@我</a>
</li>
</ul>
</li>
</ul>
<script>
$(function() {
// 滑鼠經過
$('.nav>li').mousemove(function() {
// $(this)jQuery當前元素 this不加引號
$(this).children('ul').show();
})
// 滑鼠離開
$('.nav>li').mouseout(function() {
$(this).children('ul').hide();
})
})
</script>
【案例】tab欄切換案例
分析:
- 點擊上部的li,當前li 添加current類,其余兄弟移除類,
- 點擊的同時,得到當前li 的索引號
- 讓下部里面相應索引號的item顯示,其余的item隱藏

<style>
* {
margin: 0;
padding: 0;
}
li {
list-style-type: none;
}
.tab {
width: 978px;
margin: 100px auto;
}
.tab_list {
height: 39px;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
.tab_list li {
float: left;
height: 39px;
line-height: 39px;
padding: 0 20px;
text-align: center;
cursor: pointer;
}
.tab_list .current {
background-color: #c81623;
color: #fff;
}
.item_info {
padding: 20px 0 0 20px;
}
.item {
display: none;
}
</style>
<script src="./jQuery.min.js"></script>
<div class="tab">
<div class="tab_list">
<ul>
<li class="current">商品介紹</li>
<li>規格與包裝</li>
<li>售后保障</li>
<li>商品評價(50000)</li>
<li>手機社區</li>
</ul>
</div>
<div class="tab_con">
<div class="item" style="display: block;">
商品介紹模塊內容
</div>
<div class="item">
規格與包裝模塊內容
</div>
<div class="item">
售后保障模塊內容
</div>
<div class="item">
商品評價(50000)模塊內容
</div>
<div class="item">
手機社區模塊內容
</div>
</div>
</div>
<script>
$(function() {
// 1.點擊上部的li,當前li 添加current類,其余兄弟移除類
$(".tab_list li").click(function() {
// 鏈式編程操作
$(this).addClass("current").siblings().removeClass("current");
// 2.點擊的同時,得到當前li 的索引號
var index = $(this).index();
console.log(index);
// 3.讓下部里面相應索引號的item顯示,其余的item隱藏
$(".tab_con .item").eq(index).show().siblings().hide();
});
})
</script>
【案例】微博發布
要求:
- 點擊發布按鈕, 動態創建一個小li,放入文本框的內容和洗掉按鈕, 并且添加到ul 中,
- 點擊的洗掉按鈕,可以洗掉當前的微博留言,
效果圖:

<style>
* {
margin: 0;
padding: 0
}
ul {
list-style: none
}
.box {
width: 600px;
margin: 100px auto;
border: 1px solid #000;
padding: 20px;
}
textarea {
width: 450px;
height: 160px;
outline: none;
resize: none;
}
ul {
width: 450px;
padding-left: 80px;
}
ul li {
line-height: 25px;
border-bottom: 1px dashed #cccccc;
display: none;
}
input {
float: right;
}
ul li a {
float: right;
}
</style>
<script src="./jQuery.min.js"></script>
<div class="box" id="weibo">
<span>微博發布</span>
<textarea name="" class="txt" cols="30" rows="10"></textarea>
<button class="btn">發布</button>
<ul></ul>
</div>
<script>
$(function() {
// 1. 點擊發布按鈕,動態創建一個小li,放入文本框的內容和洗掉按鈕,并且添加到ul中
$(".btn").on("click",function() {
var li = $("<li></li>");
li.html($(".txt").val() + "<a href = 'javascript:;'>洗掉</a>");
$("ul").prepend(li);
li.slideDown(); //下滑,更好的顯示效果
$(".txt").val("");
})
// 2. 點擊洗掉按鈕,可以洗掉當前的微博留言li
// $("ul a").click(function() { //此時的click事件不能給動態創建的a添加事件
// alert(11);
// })
// on可以給動態創建的元素系結事件
$("ul").on("click","a",function() {
$(this).parent().slideUp(function() {
$(this).remove
})
})
})
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/290456.html
標籤:其他
上一篇:所有帖子的 分類 總結
下一篇:vue階段小實訓第一周面試題
