2

我知道.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 是行不通的。

任何帮助将不胜感激!

谢谢!

4

3 回答 3

5

on 的语法是

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

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

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

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

于 2012-10-24T03:47:21.810 回答
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');
});
于 2012-10-24T03:48:14.150 回答
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.

于 2012-10-24T03:50:30.813 回答