jquery bind用法

  1. bind介紹
    bind() 方法爲被選元素添加一個或多個事件處理程序,並規定事件發生時運行的函數。
  2. 語法

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

    event 必須。添加到元素的一個或多個事件如:click,mouseover,mouseup,change,select
    data 可不填。傳遞到函數的額外數據,如:$(selector).bind(“click”,”input”,function(){});
    function(){} 必填。綁定事件觸發的函數
  3. 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('我是第三個點擊事件');
    });

    // unbind() 刪除點擊事件
    $('p').bind('click',function(){
        $('button').unbind('click');
        alert('button 的點擊事件被刪除');
    });
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章