這篇文章主要介紹了jQuery中bind()方法用法,實(shí)例分析了bind()方法的功能、定義及為匹配元素的特定事件綁定事件處理方法的技巧,需要的朋友可以參考下
本文實(shí)例講述了jQuery中bind()方法用法。分享給大家供大家參考。具體分析如下:
此方法為匹配元素的特定事件綁定事件處理方法。
在語法上bind()方法和one()方法是一樣的,它們的不同在于綁定的處理方法的執(zhí)行次數(shù)。
語法結(jié)構(gòu)一:
復(fù)制代碼代碼如下:
$(selector).bind(type,data,function)
參數(shù)列表: 參數(shù) | 描述 |
type | 定義綁定到匹配元素中的事件類型。 如果有多個(gè)事件使用空格分隔。 |
data | 可選。傳遞給事件對(duì)象的額外數(shù)據(jù)對(duì)象。 |
function | 定義當(dāng)事件發(fā)生時(shí)運(yùn)行的方法。 |
實(shí)例代碼:
復(fù)制代碼代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.survivalescaperooms.com/" />
<title>bind()函數(shù)-武林網(wǎng)</title>
<style type="text/css">
div{
width:100px;
height:100px;
border:1px solid blue;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").bind("click",function(){$("div").css({color:"green",fontSize:"20px"})});
})
</script>
</head>
<body>
<div>武林網(wǎng)歡迎您</div>
</body>
</html>
語法結(jié)構(gòu)二:
復(fù)制代碼代碼如下:
$(selector).bind({type:function, type:function, ...})
參數(shù)列表:
參數(shù) | 描述 |
{type:function, type:function, ...} | 定義綁定到匹配元素的事件和事件處理方法。 type與function參數(shù)描述如上表。 |
實(shí)例代碼:
復(fù)制代碼代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.survivalescaperooms.com/" />
<title>bind()函數(shù)-武林網(wǎng)</title>
<style type="text/css">
div{
width:100px;
height:100px;
border:1px solid red
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").bind({click:function(){$("div").css("color","green")},
mouseover:function(){$("div").css("background-color","blue")},
dblclick:function(){$("div").css("background-color","red")}
})
})
</script>
</head>
<body>
<div>武林網(wǎng)歡迎您</div>
</body>
</html>
希望本文所述對(duì)大家的jQuery程序設(shè)計(jì)有所幫助。
本文轉(zhuǎn)自:螞蟻部落http://www.softwhy.com/