您的位置:首页 > Web前端 > JQuery

jquery bind用法

2017-02-13 13:36 309 查看
bind介绍

bind() 方法为被选元素添加一个或多个事件处理程序,并规定事件发生时运行的函数。

语法

`$(selector).bind(event,data,function)


event 必须。添加到元素的一个或多个事件如:click,mouseover,mouseup,change,select

data 可不填。传递到函数的额外数据,如:$(selector).bind(“click”,”input”,function(){});

function(){} 必填。绑定事件触发的函数

bind绑定多个函数

$("button").bind({ // 注意它的格式是 json
click:function(){$("div").css("border","5px solid orange");},
mouseover:function(){$("div").css("background-color","red");},
mouseout:function(){$("div").css("background-color","#FFFFFF");}
});


4.bind绑定数据

// bind() 绑定 click 事件传 参数2 并且打印出 参数2
$('button').bind('click',['路飞','索隆','乌索普'],function(event){
alert(event.data[0]); // 路飞
});


5.unbind bind事件移除

html 代码
<button>unbind()</button>
<p>点我删除上边按钮的事件</p>

js 代码
// bind() 绑定多个点击事件
$('button').click(function(){
alert('我是第一个点击事件');
});

$('button').click(function(){
alert('我是第二个点击事件');
});

$('button').bind('click',function(){
alert('我是第三个点击事件');
});

// un
4000
bind() 删除点击事件
$('p').bind('click',function(){
$('button').unbind('click');
alert('button 的点击事件被删除');
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: