我想将.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;
}
});
答案 0 :(得分:1)
$(".delete").live('click',function()
可以转换为
$(document).on('click', ".delete", function()
答案 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 function和jQuery - how to use the “on()” method instead of “live()”?
答案 2 :(得分:0)
$("#someArbitraryContextNode").on('click',".delete",function(){});