如何在.jQuery中将.live转换为.on

时间:2013-04-15 06:33:39

标签: jquery

我想将.live转换为.on方法,因为.live使用不多,但我无法转换它。请帮我改变它。

$(".delete").live('click', function () {
    var id = $(this).attr('id');
    var b = $(this).parent().parent();
    var dataString = 'id=' + id;
    if (confirm("Sure you want to delete this update?")) {
        $.ajax({
            type: "POST",
            url: "delete.php",
            data: dataString,
            cache: false,
            success: function (e) {
                b.hide();
                e.stopImmediatePropagation();
            }
        });
        return false;
    }
});

3 个答案:

答案 0 :(得分:1)

$(".delete").live('click',function()

可以转换为

$(document).on('click', ".delete", function()

Documentation

答案 1 :(得分:1)

变化:

$(".delete").live('click',function()

要:

$("body").on('click', '.delete', function () {

解决方案:

$("body").on('click', '.delete', function () {
    var id = $(this).attr('id');
    var b = $(this).parent().parent();
    var dataString = 'id=' + id;
    if (confirm("Sure you want to delete this update?")) {
        $.ajax({
            type: "POST",
            url: "delete.php",
            data: dataString,
            cache: false,
            success: function (e) {
                b.hide();
                e.stopImmediatePropagation();
            }
        });
        return false;
    }
});

另请参阅jQuery 1.9 .live() is not a functionjQuery - how to use the “on()” method instead of “live()”?

答案 2 :(得分:0)

$("#someArbitraryContextNode").on('click',".delete",function(){});