jQuery事件綁定和解綁、事件冒泡與阻止事件冒泡及彈出應(yīng)用示例
本文實(shí)例講述了jQuery事件綁定和解綁、事件冒泡與阻止事件冒泡及彈出應(yīng)用。分享給大家供大家參考,具體如下:
事件的綁定和解綁
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="../js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function () {
$('#div1').bind('mouseover click',function (event) {//綁定事件:移動(dòng)到div塊上和點(diǎn)擊
alert($(this).html);
$(this).unbind('mouseover');//取消鼠標(biāo)移動(dòng)到上面的事件
})
})
</script>
<style type="text/css">
#div1{
background-color: #f6b544;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div id="div1">
</div>
</body>
</html>
綁定事件:移動(dòng)到div塊上和點(diǎn)擊
解綁事件:取消鼠標(biāo)移動(dòng)到上面的事件
事件冒泡-阻止事件冒泡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="../js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function () {
$('.son').click(function () {
alert(1);
});
$('.father').bind('click',function () {
alert(2);
});
$('.grandfather').bind('click',function () {
alert(3);
});
})
</script>
<style type="text/css">
.grandfather{
width: 300px;
height: 300px;
background-color: green;
}
.father{
width: 200px;
height: 200px;
background-color: gold;
}
.son{
width: 100px;
height: 100px;
background-color: red;
}
</style>
</head>
<body>
<div class="grandfather">
<div class="father">
<div class="son">
</div>
</div>
</div>
</body>
</html>
定義了三個(gè)div,在son點(diǎn)擊一下彈出1,father點(diǎn)擊一下彈出2,grandfather點(diǎn)擊一下彈出3,如果我們點(diǎn)擊son一下,那么會(huì)依次彈出123,點(diǎn)擊father一下會(huì)依次彈出二三。
按照標(biāo)簽往上傳到它的父級(jí)
事件冒泡有時(shí)候不需要,需要阻止,通過(guò)eventstopPropagation()來(lái)阻止
$('.son').click(function (event) {//event就是一個(gè)事件對(duì)象
//用這個(gè)事件對(duì)象就能使用事件對(duì)象的方法
alert(1);
event.stopPropagation();//阻止事件對(duì)象冒泡
});
除了阻止事件冒泡,還要阻止一些默認(rèn)行為,開(kāi)發(fā)中直接return false就行。
$('.father').bind('click',function () {
alert(2);
//阻止事件冒泡和阻止默認(rèn)行為的同意寫(xiě)法
return false;
});
彈框
點(diǎn)擊彈框外面關(guān)閉彈框
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="../js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function () {
$('#btn').click(function () {
// alert(2);
$('.pop_con').fadeIn();
// alert(1);
return false;//阻止事件,冒泡
});
$(document).click(function () {
$('.pop_con').fadeOut();
})
})
</script>
</head>
<style type="text/css">
.pop{
position: fixed;
width: 500px;
height: 300px;
background-color: #fff;
border: 3px solid #000;
left: 50%;
top: 50%;
margin-left: -250px;
margin-top: -150px;/*拉回去*/
z-index: 2;
}
.mask{
position: fixed;
width: 100%;
height: 100%;
background-color: #000000;
opacity: 0.3;
filter:alpha(opacity=30);/*兼容ie瀏覽器的*/
left: 0;
top: 0;
z-index: 1;/*z-index設(shè)置現(xiàn)實(shí)層級(jí)*/
}
.pop_con{
display: none;/*因?yàn)閜op_con包含住了mask和pop,所以設(shè)置了這個(gè)之后,他們就隱藏了*/
}
</style>
<body>
<input type="button" name="" value="彈出" id="btn">
<div class="pop_con">
<div class="pop">
彈框里面的文字
</div>
<div class="mask"></div>
</div>
</body>
</html>
如果不組織事件冒泡的話,點(diǎn)擊之后,彈框出現(xiàn)之后,就會(huì)直接隱藏,只有阻止之后,才能點(diǎn)擊外框的document或者mask才能隱藏彈框。
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery常見(jiàn)事件用法與技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery操作json數(shù)據(jù)技巧匯總》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常見(jiàn)經(jīng)典特效匯總》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- jQuery?事件綁定及取消?bind?live?delegate?on?one區(qū)別解析
- jquery事件綁定方法介紹
- jQuery實(shí)現(xiàn)的事件綁定功能基本示例
- jQuery的三種bind/One/Live/On事件綁定使用方法
- jQuery 全選 全不選 事件綁定的實(shí)現(xiàn)代碼
- jQuery事件綁定方法學(xué)習(xí)總結(jié)(推薦)
- jquery移除了live()、die(),新版事件綁定on()、off()的方法
- 關(guān)于Jquery中的事件綁定總結(jié)
- jquery事件綁定解綁機(jī)制源碼解析
- jQuery事件綁定用法詳解
- 深入理解jQuery事件綁定
- jQuery事件綁定on()與彈窗實(shí)現(xiàn)代碼
- jQuery事件綁定用法詳解(附bind和live的區(qū)別)
- jQuery實(shí)現(xiàn)按鈕只點(diǎn)擊一次后就取消點(diǎn)擊事件綁定的方法
- JQuery中DOM事件綁定用法詳解
- jQuery事件綁定on()、bind()與delegate() 方法詳解
- jQuery事件綁定與解除綁定實(shí)現(xiàn)方法
- jquery中click等事件綁定及移除的幾種方法小結(jié)
相關(guān)文章
JQuery實(shí)現(xiàn)簡(jiǎn)單驗(yàn)證碼提示解決方案
驗(yàn)證碼提示功能在ui界面的登陸及提交窗口中起到了一定的有好作用,極大的提高了用戶體驗(yàn),本文帶著這個(gè)美好的祝愿為您實(shí)現(xiàn)簡(jiǎn)單驗(yàn)證碼提示,需要的朋友可以了解下2012-12-12
基于jQuery實(shí)現(xiàn)Div窗口震動(dòng)特效代碼-代碼簡(jiǎn)單
本文給大家介紹基于jiquery實(shí)現(xiàn)div窗口震動(dòng)特效代碼,需要的朋友可以參考下2015-08-08
jquery mobile實(shí)現(xiàn)撥打電話功能的幾種方法
在做一個(gè)便民服務(wù)電話,用到移動(dòng)web中列出的電話,點(diǎn)擊需要實(shí)現(xiàn)調(diào)用通訊錄,撥打電話功能,有需要的朋友可以參考一下2013-08-08
jquery庫(kù)或JS文件在eclipse下報(bào)錯(cuò)問(wèn)題解決方法
在工程中導(dǎo)入jquery-1.7.1之后一直有一個(gè)紅叉叉,雖然不會(huì)影響程序功能,但是看著非常不舒服,下面有個(gè)不錯(cuò)的解決方法,大家可以嘗試下2014-04-04
JQuery 獲取多個(gè)select標(biāo)簽option的text內(nèi)容(實(shí)例)
下面小編就為大家?guī)?lái)一篇JQuery 獲取多個(gè)select標(biāo)簽option的text內(nèi)容(實(shí)例)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
jQuery html()方法使用不了無(wú)法顯示內(nèi)容的問(wèn)題
jquery中的html方法使用不了,只能用完最基本的innerHTML把內(nèi)容展示出來(lái)2014-08-08
自編jQuery插件實(shí)現(xiàn)模擬alert和confirm
現(xiàn)在絕大多數(shù)網(wǎng)站都不用自帶的alert和confirm了,因?yàn)榻缑嫣擦?。因此這個(gè)插件就這樣產(chǎn)生了...2014-09-09
jQuery 聯(lián)動(dòng)日歷實(shí)現(xiàn)代碼
首先還是感謝下 妙味課堂 錄制的這一個(gè)日歷聯(lián)動(dòng)的視頻,正好整理下,方便需要的朋友2012-05-05

