jQuery阻止事件冒泡具體實(shí)現(xiàn)
下面是html代碼部分:
<body>
<div id="content">
外層div元素
<span>內(nèi)層span元素</span>
外層div元素
</div>
<div id="msg"></div>
</body>
對應(yīng)的jQuery代碼如下:
<script type="text/javascript">
$(function(){
// 為span元素綁定click事件
$('span').bind("click",function(){
var txt = $('#msg').html() + "<p>內(nèi)層span元素被點(diǎn)擊.<p/>";//獲取html信息
$('#msg').html(txt);// 設(shè)置html信息
});
// 為div元素綁定click事件
$('#content').bind("click",function(){
var txt = $('#msg').html() + "<p>外層div元素被點(diǎn)擊.<p/>";
$('#msg').html(txt);
});
// 為body元素綁定click事件
$("body").bind("click",function(){
var txt = $('#msg').html() + "<p>body元素被點(diǎn)擊.<p/>";
$('#msg').html(txt);
});
})
</script>
當(dāng)點(diǎn)擊span時(shí),會(huì)觸發(fā)div與body 的點(diǎn)擊事件。點(diǎn)擊div時(shí)會(huì)觸發(fā)body的點(diǎn)擊事件。
如何防止這種冒泡事件發(fā)生呢?
修改如下:
<script type="text/javascript">
$(function(){
// 為span元素綁定click事件
$('span').bind("click",function(event){
var txt = $('#msg').html() + "<p>內(nèi)層span元素被點(diǎn)擊.<p/>";
$('#msg').html(txt);
event.stopPropagation(); // 阻止事件冒泡
});
// 為div元素綁定click事件
$('#content').bind("click",function(event){
var txt = $('#msg').html() + "<p>外層div元素被點(diǎn)擊.<p/>";
$('#msg').html(txt);
event.stopPropagation(); // 阻止事件冒泡
});
// 為body元素綁定click事件
$("body").bind("click",function(){
var txt = $('#msg').html() + "<p>body元素被點(diǎn)擊.<p/>";
$('#msg').html(txt);
});
})
</script>
event.stopPropagation(); // 阻止事件冒泡
有時(shí)候點(diǎn)擊提交按鈕會(huì)有一些默認(rèn)事件。比如跳轉(zhuǎn)到別的界面。但是如果沒有通過驗(yàn)證的話,就不應(yīng)該跳轉(zhuǎn)。這時(shí)候可以通過設(shè)置event.preventDefault(); //阻止默認(rèn)行為 ( 表單提交 )。
下面是案例:
<script type="text/javascript">
$(function(){
$("#sub").bind("click",function(event){
var username = $("#username").val(); //獲取元素的值,val() 方法返回或設(shè)置被選元素的值。
if(username==""){ //判斷值是否為空
$("#msg").html("<p>文本框的值不能為空.</p>"); //提示信息
event.preventDefault(); //阻止默認(rèn)行為 ( 表單提交 )
}
})
})
</script>
html部分:
<body>
<form action="test.html">
用戶名:<input type="text" id="username" />
<br/>
<input type="submit" value="提交" id="sub"/>
</form>
<div id="msg"></div>
</body>
還有一種防止默認(rèn)行為的方法就是return false。效果一樣。
代碼如下:
<script type="text/javascript">
$(function(){
$("#sub").bind("click",function(event){
var username = $("#username").val(); //獲取元素的值
if(username==""){ //判斷值是否為空
$("#msg").html("<p>文本框的值不能為空.</p>"); //提示信息
return false;
}
})
})
</script>
同理,上面的冒泡事件也可以通過return false來處理。
<script type="text/javascript">
$(function(){
// 為span元素綁定click事件
$('span').bind("click",function(event){
var txt = $('#msg').html() + "<p>內(nèi)層span元素被點(diǎn)擊.<p/>";
$('#msg').html(txt);
return false;
});
// 為div元素綁定click事件
$('#content').bind("click",function(event){
var txt = $('#msg').html() + "<p>外層div元素被點(diǎn)擊.<p/>";
$('#msg').html(txt);
return false;
});
// 為body元素綁定click事件
$("body").bind("click",function(){
var txt = $('#msg').html() + "<p>body元素被點(diǎn)擊.<p/>";
$('#msg').html(txt);
});
})
</script>
相關(guān)文章
jquery實(shí)現(xiàn)手風(fēng)琴效果
這篇文章主要介紹了jquery實(shí)現(xiàn)手風(fēng)琴效果,像手風(fēng)琴一樣打開,立體感效果比較強(qiáng),感興趣的小伙伴們可以參考一下2015-11-11鋒利的jQuery 第三章章節(jié)總結(jié)的例子
改寫《鋒利的jQuery》第三章章節(jié)總結(jié)的例子2010-03-03使用CDN和AJAX加速WordPress中jQuery的加載
這篇文章主要介紹了使用CDN和AJAX加速WordPress中jQuery的加載的方法,注意一下WordPress中以及CDN的Google連接在內(nèi)地的網(wǎng)絡(luò)問題,需要的朋友可以參考下2015-12-12jQuery 獲取除某指定對象外的其他對象 ( :not() 與.not())
這篇文章主要介紹了JQuery 獲取除某指定對象外的其他對象 ( :not() 與.not()),需要的朋友可以參考下2018-10-10利用JQUERY實(shí)現(xiàn)多個(gè)AJAX請求等待的實(shí)例
下面小編就為大家分享一篇利用JQUERY實(shí)現(xiàn)多個(gè)AJAX請求等待的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12jQuery插件實(shí)現(xiàn)適用于移動(dòng)端的地址選擇器
本文給大家分享的是一個(gè)jQuery插件,實(shí)現(xiàn)的是適用于移動(dòng)端的地址選擇器,有需要的小伙伴可以拿走參考下。2016-02-02