先看一段代码,这里函数处理ajax请求的返回结果,将表格的内容先移除然后重新添加
function dispose(data){
var data = eval('('+ data +')');
var dataJson = data.Json;
if(data.state == 'success'){
$('#bugboardTable tbody').empty().prepend(bugBoardData.bugBoardData(dataJson).str);
var length = bugBoardData.bugBoardData(dataJson).i;
$('.pageNum em').html('共'+ length +'条');
poolVamp('#container');
。。。。。
}
};
这么做导致的问题是 table中有一列绑定了事件 但是remove后不只是旧的数据 连绑定的事件也会被清除,所以只能重新绑定
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
比较一下jquery删除内容的方法
1、empty()
源码
empty: function() {
var elem,
i = 0;
for ( ; (elem = this[i]) != null; i++ ) {
// Remove element nodes and prevent memory leaks
if ( elem.nodeType === 1 ) {
jQuery.cleanData( getAll( elem, false ) );
}
// Remove any remaining nodes
while ( elem.firstChild ) {
elem.removeChild( elem.firstChild );
}
// If this is a select, ensure that it displays empty (#12336)
// Support: IE<9
if ( elem.options && jQuery.nodeName( elem, "select" ) ) {
elem.options.length = 0;
}
}
return this;
},
清除这个标签内的所有内容,为了防止内存泄漏并且会清空数据和事件
To avoid memory leaks, jQuery removes other constructs such as data and event handlers from the child elements before removing the elements themselves.
(关于jquery内存泄漏---->