$(".aa").on("click",function(){ alert(1) }).on("mousemove",function(){ console.log(1); });
$(".aa").on({click:function(){ alert(1) },mousemove:function(){ console.log(2); }})
若是須要傳參數,除定義全局變量,在方法中直接使用全局變量外,還可以使用如下方法spa
$(".aa").on("click",{val:a},a1).on("mousemove",{val:b},a2); function a1(e){ alert(e.data.val) } function a2(e){ console.log(e.data.val) }
經過delegate給同一元素綁定多個事件code
$("body").delegate( ".msg-layer",{ mousedown: function (e) { var el = $(".msg-layer"); var os = el.offset(), dx = e.pageX - os.left, dy = e.pageY - os.top; $(document).on('mousemove.drag', function (e) { el.offset({top: e.pageY - dy, left: e.pageX - dx}); }); }, mouseup: function (e) { $(document).off('mousemove.drag'); } } );
$("body").delegate( ".msg-layer","mousedown mouseup",function(e){ if(e.type == "mousedown"){ var el = $(".msg-layer"); var os = el.offset(), dx = e.pageX - os.left, dy = e.pageY - os.top; $(document).on('mousemove.drag', function (e) { el.offset({top: e.pageY - dy, left: e.pageX - dx}); }); }else if(e.type == "mouseup"){ $(document).off('mousemove.drag'); } } );