通过映射方法绑定不同事件
html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script>
<script src="./bind.js" type="text/javascript"></script>
<link type="text/css" href="./bind.css" />
<title>通过映射方法绑定不同事件</title>
</head>
<body>
<div>姓名:<input type="text" class="txt" /></div>
<div id="divtip" class="clstip"></div>
</body>
</html>
css
body{
font-size: 13px;
}
.clstip{
border: #CCCCCC 1px solid;
background-color: #EEEEEE;
margin-top: 15px;
padding: 5px;
width: 185px;
display: none;
}
.txt{
padding: 3px;
border: #666666 1px solid;
}
JavaScript
在bind方法中,event.data作为参数传递到了事件中
$(function(){
var message = "当前执行的是focus事件!";
$(".txt").bind("focus", {
msg : message} , function(event){
$("#divtip").show().html(event.data.msg);//设置文本
});
var message = "当前执行的是change事件!";
$(".txt").bind("change", {
msg : message} , function(event){
$("#divtip").show().html(event.data.msg);//设置文本
})
})
普通的bind()方法
$(function(){
$(".txt").bind({
focus:function(){
$("#divtip").show().html("当前执行的是focus事件!");
},change:function(){
$("#divtip").show().html("当前执行的是change事件!");
}
})
})
总结
我认为绑定方法可以用JavaScript或者jQuery,在jQuery中bind方法三个参数 > bind( type, [data] ,fn) data为可选值,所以关注的应该是type,要自定义的方法(字符串类型),fn为选择元素的事件中的处理函数(这个好难判断)