jquery 事件執(zhí)行檢測代碼
更新時間:2009年12月09日 04:20:30 作者:
在寫Web 應(yīng)用,或者Web Ajax功能的時候我們經(jīng)常需要處理事件,jquery已經(jīng)非常完美的封裝了事件,但是有時候事件的執(zhí)行順序還是會有微小差別
前兩天做城市搜索的時候,我對搜索按鈕綁定了事件,如果輸入欄內(nèi)的文字不符合要求就用標(biāo)簽提示(標(biāo)簽展示出來后對document綁定click,點(diǎn)擊后隱藏標(biāo)簽)并使輸入欄獲得焦點(diǎn)。就這樣一個小功能我調(diào)試了、3個多小時,IE 里表現(xiàn)的是標(biāo)簽一閃而過,后來發(fā)現(xiàn)是按鈕的點(diǎn)擊事件和input的交替出發(fā)了多次,估計是事件回退發(fā)生的,用了preventDefault() 和 stopPropagation()兩個函數(shù)后修正了問題。雖然問題解決了,但是事件的執(zhí)行順序真是很難掌握呀,于是我做了個簡單的事件監(jiān)測函數(shù),可以自動記錄頁面事件觸發(fā),見如下代碼(首先肯定要引入jquery!...)
//事件執(zhí)行監(jiān)測
function eventsMonitor(op){
var defaultSetting = {
eventsStr: "click focus blur",
splitStr: " ",
css:{
"border":"1px red solid",
"z-index":9000000,
"background":"white",
"position":"absolute",
width:400,
height:200,
"overflow-x":"hidden",
"overflow-y":"auto"
}
};
var ops = $.extend(true,defaultSetting,op);
$('<div id="DivForLogEvents"><div></div><div>').appendTo("body").css(ops.css);
var $infolog = $("#DivForLogEvents div:eq(0)");
$.each(ops.eventsStr.split(ops.splitStr),function(i,v){
if(v != 'resize')
$("*:not('#DivForLogEvents')").bind(v, function(e){
if(!$(e.target).is("#DivForLogEvents") && !$(e.target).is($infolog)){
$infolog.append((e.target.nodeName||" ") + "->" + (e.target.id||e.target.Name||" ") + " "+v+" event!<br>");
$("#DivForLogEvents:not(:animated)").animate({scrollTop:$infolog.height()},300);
}
});
else
$(window).bind('resize', function(e){
if(!$(e.target).is("#DivForLogEvents") && !$(e.target).is($infolog)){
$infolog.append((e.target.nodeName||" ") + "->" + (e.target.id||e.target.Name||" ") + " "+v+" event!<br>");
$("#DivForLogEvents:not(:animated)").animate({scrollTop:$infolog.height()},300);
}
});
});
}
調(diào)用方法示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script><!--Google CDN-->
<script type="text/javascript" src="eventsMonitor.js"></script><!--Google CDN-->
<script type="text/javascript">
$(document).ready(function(){
eventsMonitor({
css:{top:0,right:20},
eventsStr:"click blur focus select scroll mousewheel resize"
});
$("#info").click(function(){$(this).text($("#DivForLogEvents").outerHeight());});
});
</script>
</head>
<body>
Hello world
<img src="/upload/2009-12/20091209042029557.gif" />
<input type="text" id="name" value="測試" />
<span id="info">ffffffff</span>
</body>
</html>
復(fù)制代碼 代碼如下:
//事件執(zhí)行監(jiān)測
function eventsMonitor(op){
var defaultSetting = {
eventsStr: "click focus blur",
splitStr: " ",
css:{
"border":"1px red solid",
"z-index":9000000,
"background":"white",
"position":"absolute",
width:400,
height:200,
"overflow-x":"hidden",
"overflow-y":"auto"
}
};
var ops = $.extend(true,defaultSetting,op);
$('<div id="DivForLogEvents"><div></div><div>').appendTo("body").css(ops.css);
var $infolog = $("#DivForLogEvents div:eq(0)");
$.each(ops.eventsStr.split(ops.splitStr),function(i,v){
if(v != 'resize')
$("*:not('#DivForLogEvents')").bind(v, function(e){
if(!$(e.target).is("#DivForLogEvents") && !$(e.target).is($infolog)){
$infolog.append((e.target.nodeName||" ") + "->" + (e.target.id||e.target.Name||" ") + " "+v+" event!<br>");
$("#DivForLogEvents:not(:animated)").animate({scrollTop:$infolog.height()},300);
}
});
else
$(window).bind('resize', function(e){
if(!$(e.target).is("#DivForLogEvents") && !$(e.target).is($infolog)){
$infolog.append((e.target.nodeName||" ") + "->" + (e.target.id||e.target.Name||" ") + " "+v+" event!<br>");
$("#DivForLogEvents:not(:animated)").animate({scrollTop:$infolog.height()},300);
}
});
});
}
調(diào)用方法示例
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script><!--Google CDN-->
<script type="text/javascript" src="eventsMonitor.js"></script><!--Google CDN-->
<script type="text/javascript">
$(document).ready(function(){
eventsMonitor({
css:{top:0,right:20},
eventsStr:"click blur focus select scroll mousewheel resize"
});
$("#info").click(function(){$(this).text($("#DivForLogEvents").outerHeight());});
});
</script>
</head>
<body>
Hello world
<img src="/upload/2009-12/20091209042029557.gif" />
<input type="text" id="name" value="測試" />
<span id="info">ffffffff</span>
</body>
</html>
效果截圖
您可能感興趣的文章:
- JQuery實(shí)現(xiàn)當(dāng)鼠標(biāo)停留在某區(qū)域3秒后自動執(zhí)行
- JQuery給元素綁定click事件多次執(zhí)行的解決方法
- jquery阻止后續(xù)事件只執(zhí)行第一個事件
- jQuery綁定事件不執(zhí)行但alert后可以正常執(zhí)行
- jquery $.ajax各個事件執(zhí)行順序
- JQuery 給元素綁定click事件多次執(zhí)行的解決方法
- 淺析jquery如何判斷滾動條滾到頁面底部并執(zhí)行事件
- Jquery on方法綁定事件后執(zhí)行多次的解決方法
- JQuery中DOM加載與事件執(zhí)行實(shí)例分析
- 淺談js在html中的加載執(zhí)行順序,多個jquery ready執(zhí)行順序
- 使用jQuery加載html頁面到指定的div實(shí)現(xiàn)方法
- Jquery在指定DIV加載HTML示例代碼
- jQuery實(shí)現(xiàn)在HTML文檔加載完畢后自動執(zhí)行某個事件的方法
相關(guān)文章
JQuery CheckBox(復(fù)選框)操作方法匯總
這篇文章主要介紹了JQuery CheckBox(復(fù)選框)操作方法匯總,本文講解了獲取單個checkbox選中項、獲取多個checkbox選中項、設(shè)置第一個checkbox 為選中值、設(shè)置最后一個checkbox為選中值等內(nèi)容,需要的朋友可以參考下2015-04-04jQuery藍(lán)色風(fēng)格滑動導(dǎo)航欄代碼分享
這篇文章主要為大家詳細(xì)介紹了jQuery藍(lán)色風(fēng)格滑動導(dǎo)航欄特效,實(shí)現(xiàn)滑塊跟隨鼠標(biāo)左右滑動,簡單、時尚、大方,有需要的小伙伴可以參考下2015-08-08jQuery實(shí)現(xiàn)鼠標(biāo)跟隨提示層效果代碼(可顯示文本,Div,Table,Html等)
這篇文章主要介紹了jQuery實(shí)現(xiàn)鼠標(biāo)跟隨提示層效果代碼,具備顯示文本,Div,Table,Html等功能.涉及jQuery針對鼠標(biāo)事件及頁面元素的相關(guān)操作技巧,需要的朋友可以參考下2016-04-04