一 mouseenter跟mouseover事件的區(qū)別
<html>
<head>
<meta charset="UTF-8">
<script src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
x=0;
y=0;
$(document).ready(function(){
$("div.over").mouseover(function(){
$(".over span").text(x+=1);
});
$("div.enter").mouseenter(function(){
$(".enter span").text(y+=1);
});
});
</script>
</head>
<body>
<p>不論鼠標(biāo)指針穿過(guò)被選元素或其子元素,都會(huì)觸發(fā) mouseover 事件。</p>
<p>只有在鼠標(biāo)指針穿過(guò)被選元素時(shí),才會(huì)觸發(fā) mouseenter 事件。</p>
<div class="over" style="background-color:lightgray;padding:20px;width:40%;float:left">
<h2 style="background-color:white;">被觸發(fā)的 Mouseover 事件:<span></span></h2>
</div>
<div class="enter" style="background-color:lightgray;padding:20px;width:40%;float:right">
<h2 style="background-color:white;">被觸發(fā)的 Mouseenter 事件:<span></span></h2>
</div>
</body>
</html>
區(qū)別:
mouseover/mouseout 事件,鼠標(biāo)經(jīng)過(guò)的時(shí)候會(huì)觸發(fā)多次,每遇到一個(gè)子元素就會(huì)觸發(fā)一次。
mouseenter/mouseleave事件,鼠標(biāo)經(jīng)過(guò)的時(shí)候只會(huì)觸發(fā)一次。
二 下拉菜單的實(shí)現(xiàn)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
* {
padding: 0;
margin: 0;
}
ul {
list-style: none;
}
.wrap {
width: 330px;
height: 30px;
margin: 100px auto 0;
background-image: url(imgs/bg.jpg);
padding-left: 10px;
}
.wrap li {
float: left;
width: 100px;
height: 30px;
margin-right: 10px;
position: relative;
}
.wrap a {
color: black;
text-decoration: none;
display: block;
width: 100px;
height: 30px;
text-align: center;
line-height: 30px;
background-image: url(imgs/libg.jpg);
}
.wrap li ul {
position: absolute;
display: none;
}
</style>
<script src="js/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function () {
/* 方法一:
$(".wrap li").mouseenter(function () {
$(this).children("ul").show();
});
$(".wrap li").mouseleave(function () {
$(this).children("ul").hide();
});
*/
/* 方法二:
$(".wrap li").hover(function () {
$(this).children("ul").show();
},function () {
$(this).children("ul").hide();
});*/
/* 方法三:
$(".wrap li").hover(function () {
var $this = $(this).children("ul");
var isshow=$this.css("display");
if (isshow === "block"){
$this.hide();
}else {
$this.show();
}
});*/
//方法四:
$(".wrap li").hover(function () {
$(this).children("ul").slideToggle();
});
});
</script>
</head>
<body>
<div class="wrap">
<ul>
<li>
<a href="#">一級(jí)菜單1</a>
<ul>
<li><a href="#">二級(jí)菜單1</a></li>
<li><a href="#">二級(jí)菜單2</a></li>
<li><a href="#">二級(jí)菜單3</a></li>
</ul>
</li>
<li>
<a href="#">一級(jí)菜單1</a>
<ul>
<li><a href="#">二級(jí)菜單1</a></li>
<li><a href="#">二級(jí)菜單2</a></li>
<li><a href="#">二級(jí)菜單3</a></li>
</ul>
</li><li>
<a href="#">一級(jí)菜單1</a>
<ul>
<li><a href="#">二級(jí)菜單1</a></li>
<li><a href="#">二級(jí)菜單2</a></li>
<li><a href="#">二級(jí)菜單3</a></li>
</ul>
</li>
</ul>
</div>
</body>
</html>
總結(jié):
1 設(shè)置div,并在div中利用<ul>以及子標(biāo)簽前<li>和<a>標(biāo)簽寫出基本菜單
2 設(shè)置菜單樣式
- 清除內(nèi)外邊距以及列表樣式
- 設(shè)置div大小以及位置,背景
- 設(shè)置
<li>標(biāo)簽樣式,左浮動(dòng),大小,位置,定位 - 設(shè)置
<a>標(biāo)簽樣式,顏色,去掉下劃線,大小,字體位置,背景,定位 - 設(shè)置
<li>標(biāo)簽下,二級(jí)菜單欄<ul>標(biāo)簽定位
3 二級(jí)菜單效果實(shí)現(xiàn) - 方法一
$(".wrap li").mouseenter(function () {
$(this).children("ul").show();
});
$(".wrap li").mouseleave(function () {
$(this).children("ul").hide();
});
- 方法二
$(".wrap li").hover(function () {
$(this).children("ul").show();
},function () {
$(this).children("ul").hide();
});
- 方法三
$(".wrap li").hover(function () {
var $this = $(this).children("ul");
var isshow=$this.css("display");
if (isshow === "block"){
$this.hide();
}else {
$this.show();
}
- 方法四
$(".wrap li").hover(function () {
$(this).children("ul").slideToggle();
});
三 DOM對(duì)象跟jQuery對(duì)象相互轉(zhuǎn)換
jQuery對(duì)象轉(zhuǎn)換成DOM對(duì)象:
方式一:$(“#btn”)[0]
方式二:$(“#btn”).get(0)
DOM對(duì)象轉(zhuǎn)換成jQuery對(duì)象:
$(document) -> 把DOM對(duì)象轉(zhuǎn)成了jQuery對(duì)象
var btn = document.getElementById(“bt n”);
btn -> $(btn);