bind 方法为每个选择的元素事件绑定函数 语法格式:bind(type,[data],fn) 其中参数type为一个或多个类型字符串参数,type参数选择范围如下: blur,focus,load,scroll,unload,click, dbclick,mousedown,mouseup,mousemove,mouseover, mouseout,mouseenter,mouseleave,change,select,submit, keydowm,keypress,keyup,error 参数data是作为event.data属性值传递给事件对象额外的数据对象 参数fn是绑定到每个选择元素的事件中的处理函数 $(function(){ $(“#button1”).bind(“click”,function(){ $(this).attr(“disabled”,”disabled”); })//按钮不可用 }) 如需绑定多个事件,可以将事件用空格隔开,下面我们添加一个click 一个mouseout时间 $(function(){ $("body ,div ,#button1").bind("click mouseout",function(){ $(this).attr("disabled","disabled"); }) }) $(function(){ $(“.txt”).bind({ focus:function(){$(“#idTip”).show().html(“执行的是focus事件”);}, change:function(){$(“#idTip”).show().html(“执行的是change事件”);} }) }) 在参数bind()方法中,第二个参数data很少用到,用途是通过该参数把一些附加的信息传递给事件处理函数fn中 $(function(){ var message=”把该信息传递给Fn函数”; $(“.txt”).bind(“focus”,{msg:message}, function(event){ $(“#idTip”).show().html(even.data.msg);} );//设置文本 })
带参数绑定函数用法 function handler(event) { alert(event.data.foo); } $("p").bind("click", {foo: "bar"}, handler)