jQuery动态移除和绑定事件

时间:2023-03-10 04:30:47
jQuery动态移除和绑定事件
 function bindEvent() {
//移除绑定事件
$('.btnsp').unbind('click');
//绑定事件
$('.btnsp').bind('click', function () {
console.log('test');
});
}
$(function () {
$.ajax(url + 'api/WxHome/GetProduct', {
dataType: 'json',
type: 'get',
success: function (data) {
var html = '';
if (data.Success) {
for (var i = 0; i < data.Module.length; i++) {
html += '<li class="btnsp">' + data.Module[i].Name + '</li>';
}
$('#content').html(html);
}
//动态绑定事件(等页面内容初始化完成后先解绑事件再次绑定事件)
bindEvent();
},
error: function (xhr, type, errorThrown) {
console.log("errer:" + errorThrown);
}
});
});

  

可参考:http://www.w3school.com.cn/jquery/event_unbind.asp