jQuery事件綁定和解綁、事件冒泡與阻止事件冒泡及彈出應(yīng)用示例
本文實例講述了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) {//綁定事件:移動到div塊上和點擊
alert($(this).html);
$(this).unbind('mouseover');//取消鼠標移動到上面的事件
})
})
</script>
<style type="text/css">
#div1{
background-color: #f6b544;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div id="div1">
</div>
</body>
</html>
綁定事件:移動到div塊上和點擊
解綁事件:取消鼠標移動到上面的事件
事件冒泡-阻止事件冒泡
<!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>
定義了三個div,在son點擊一下彈出1,father點擊一下彈出2,grandfather點擊一下彈出3,如果我們點擊son一下,那么會依次彈出123,點擊father一下會依次彈出二三。
按照標簽往上傳到它的父級
事件冒泡有時候不需要,需要阻止,通過eventstopPropagation()來阻止
$('.son').click(function (event) {//event就是一個事件對象
//用這個事件對象就能使用事件對象的方法
alert(1);
event.stopPropagation();//阻止事件對象冒泡
});
除了阻止事件冒泡,還要阻止一些默認行為,開發(fā)中直接return false就行。
$('.father').bind('click',function () {
alert(2);
//阻止事件冒泡和阻止默認行為的同意寫法
return false;
});
彈框
點擊彈框外面關(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)實層級*/
}
.pop_con{
display: none;/*因為pop_con包含住了mask和pop,所以設(shè)置了這個之后,他們就隱藏了*/
}
</style>
<body>
<input type="button" name="" value="彈出" id="btn">
<div class="pop_con">
<div class="pop">
彈框里面的文字
</div>
<div class="mask"></div>
</div>
</body>
</html>
如果不組織事件冒泡的話,點擊之后,彈框出現(xiàn)之后,就會直接隱藏,只有阻止之后,才能點擊外框的document或者mask才能隱藏彈框。
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery常見事件用法與技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery操作json數(shù)據(jù)技巧匯總》、《jQuery擴展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計有所幫助。
- jQuery?事件綁定及取消?bind?live?delegate?on?one區(qū)別解析
- jquery事件綁定方法介紹
- jQuery實現(xiàn)的事件綁定功能基本示例
- jQuery的三種bind/One/Live/On事件綁定使用方法
- jQuery 全選 全不選 事件綁定的實現(xiàn)代碼
- jQuery事件綁定方法學(xué)習(xí)總結(jié)(推薦)
- jquery移除了live()、die(),新版事件綁定on()、off()的方法
- 關(guān)于Jquery中的事件綁定總結(jié)
- jquery事件綁定解綁機制源碼解析
- jQuery事件綁定用法詳解
- 深入理解jQuery事件綁定
- jQuery事件綁定on()與彈窗實現(xiàn)代碼
- jQuery事件綁定用法詳解(附bind和live的區(qū)別)
- jQuery實現(xiàn)按鈕只點擊一次后就取消點擊事件綁定的方法
- JQuery中DOM事件綁定用法詳解
- jQuery事件綁定on()、bind()與delegate() 方法詳解
- jQuery事件綁定與解除綁定實現(xiàn)方法
- jquery中click等事件綁定及移除的幾種方法小結(jié)
相關(guān)文章
為jQuery.Treeview添加右鍵菜單的實現(xiàn)代碼
jquery.treeview 數(shù)據(jù)通過JSON數(shù)據(jù)加載,有需要的朋友,可以通jquery的AJAX方法獲取相關(guān)的JSON數(shù)據(jù)。2010-10-10
jquery checkbox,radio是否選中的判斷代碼
jquery checkbox,radio是否選中的判斷代碼,需要的朋友可以參考下。2010-03-03
jquery創(chuàng)建并行對象或者合并對象的實現(xiàn)代碼
如果有對象A ,B 現(xiàn)在我想要合并成對象C 從C里面可以找到A , B 及其子對象 怎么做2012-10-10
JQuery使用$.ajax和checkbox實現(xiàn)下次不在通知功能
這篇文章主要介紹了JQuery使用$.ajax和checkbox實現(xiàn)下次不在通知功能,本文給出HTML代碼、JS代碼、和后端JAVA代碼完整示例,需要的朋友可以參考下2015-04-04
jQuery EasyUI 開源插件套裝 完全替代ExtJS
JQuery愛好者們的福音 jQuery EasyUI 開源插件套裝 完全替代ExtJS(引用)2010-03-03

