从.live更改为.on

时间:2012-10-24 03:45:08

标签: javascript jquery jquery-on

我知道.live已被弃用,最近我正在更新页面并意识到我正在使用.live我想切换到.on但不明白要改变什么。这是我目前的代码:

    //Script for Choosing which form to display
$("#email-button, #text-button").live('click',
function(){  

    //figure out what button was clicked. 
    if(this.id === "email-button"){
        var btnA = $(this);
        var btnB = $("#text-button");
        var divA = $('#email-form');
        var divB = $('#text-form');
    }
    else{
        btnA = $(this);
        btnB = $("#email-button");
        divA = $('#text-form');
        divB = $('#email-form');
    }

    //make sure it is not already active, no use to show/hide when it is already set
    if(btnA.hasClass('dark_button_span')){
        return; 
    }

    //see if div is visible, if so hide, than show first div
    if(divB.is(":visible")){        
        divB.fadeOut("slow", function(){
             divA.fadeIn("slow");
        });
    }
    else{//if already hidden, just show the first div
        divA.fadeIn("slow");            
    }

    //Add and remove classes to the buttons to switch state
    btnA.addClass('dark_button_span').removeClass('light_button_span');
    btnB.removeClass('dark_button_span').addClass('light_button_span');
  }    
);

我在编写上述脚本时有帮助,不知道要改变什么。简单地将.live更改为.on不起作用。

任何帮助将不胜感激!

谢谢!

3 个答案:

答案 0 :(得分:5)

on的语法是

$("containerElement").on("click", "targetElement(s)", function(){ });

所以在你的情况下它可能是

$("body").on("click", "#email-button, #text-button", function(){ });

但是比body更具体是一个好主意。

答案 1 :(得分:1)

$(document).on('click', '#email-button, #text-button', function() {
    // Your code
});

应该做的伎俩。请参阅http://api.jquery.com/live/http://api.jquery.com/on/

但是,由于您使用的是ID,因此您可能甚至不需要.live()或委派.on()。所以我写的方式很简单:

function doButtons(btnA, btnB, divA, divB) {
    btnA = $(btnA); btnB = $(btnB); divA = $(divA); divB = $(divB);

    // Make sure it is not already active, no use to show/hide when it is already set
    if (btnA.hasClass('dark_button_span'))
        return; 

    // See if div is visible, if so hide, then show first div.
    if (divB.is(":visible")) {        
        divB.fadeOut("slow", function  (){
            divA.fadeIn("slow");
        });
    }
    else // If already hidden, just show the first div.
        divA.fadeIn("slow");

    // Add and remove classes to the buttons to switch state.
    btnA.addClass('dark_button_span').removeClass('light_button_span');
    btnB.removeClass('dark_button_span').addClass('light_button_span');
}

$('#email-button').click(function () {
     doButtons(this, '#text-button', '#email-form', '#text-form');
});
$('#text-button').click(function () {
    doButtons(this, '#email-button', '#text-form', '#email-form');
});

答案 2 :(得分:1)

jQuery的.on除非您提供选择器,否则不会使用事件委派。在上面的代码中,.live会监听document处的事件,但这太冒泡了。如果我们使用.on实现它,尽管我们会执行以下操作:

var handler = function( e ) {
    console.log( "Clicked" );
};

$( document ).on( "click", "#email-button, #text-button", handler );

再说一次,听document上的事件并不是那么明智;理想情况下,您会在选择器上方选择一个元素。因此,如果#email-button#text-button有共同的父级,则应使用该代替document