jquery自定義事件的使用

  jquery自定義事件要用到2個方法

  1:bind(type,[data],fn);其中type是自定義事件類型(名稱);data是可選參數,是自定義函數fn中第一個參數中的data屬性,注意data是一個數組類型;fn是事件觸發時執行的函數。

  2:trigger(type,[data]);其中type是自定義事件類型(名稱);data是可選參數是自定義函數fn中第二個參數,注意這是一個數組類型。

  應用舉例:

  

<!doctype html>
<html lang="zh">
 <head>
 <meta charset="UTF-8">
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
 <title>Document</title>
 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
 <meta name="renderer" content="webkit|ie-comp|ie-stand">
 <meta name="keywords" content="">
 <meta name="description" itemprop="description" content="">
 <meta name="protocol" content="1">
 <meta itemprop="name" content="name">
 <meta itemprop="image" content="http://9.url.cn/edu/img/logo_pc_rich.png"/>

 <link rel="dns-prefetch" href="http://7.url.cn">
 <link rel="dns-prefetch" href="http://9.url.cn">
 <link rel="dns-prefetch" href="http://8.url.cn">
 <link rel="stylesheet" href="http://8.url.cn/edu/css/common.980ee.css"/>
 <link rel="stylesheet" href="http://8.url.cn/edu/css/index_refactor.90164.css"/>
 <link rel="shortcut icon" href="http://9.url.cn/edu/img/favicon.ico"/>
 </head>
 <body>
		<div>
				<p>
						<input type="text" id="number01" value="0">&nbsp;&nbsp;+
				</p>
				<p>
						<input type="text" id="number02" value="1">&nbsp;&nbsp;
						<input type="button" id="ok" value="=">
				</p>
				<p>
						<input type="text" id="result" value="1">&nbsp;&nbsp;
						
				</p>
					
		</div>
	<script type="text/javascript" src="js/jquery.js"></script>
	<script type="text/javascript">
	<!--
		$(window).bind("resultChange",function(event,a,b,c){
			alert(a+":"+b+":"+c);
		});
		$("#result").change(function(){
		
			var result=$(this).val();
			if(result>0)
				$(window).trigger("resultChange",[result,result,result]);
			
		});
		$("#ok").click(function(){
			$("#result").val(($("#number01").val()-0)+($("#number02").val()-0));
			$("#result").change();
		});
	//-->
	</script>
 </body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章