我有这样的代码:
_removeSelection : function(e, refocus) {
var thisObj = e.data,
lineItem = $(this).closest('li'),
templateData = lineItem.data('templateData');
thisObj.element.trigger('beforeRemoveSelection',[templateData]);
lineItem.remove();
thisObj._selection_items = $(thisObj._selections).find('li');
if ( ( refocus != 'undefined' && refocus === true ) || refocus == 'undefined') {
thisObj.refocus();
}
thisObj.element.trigger('toggleSelection');
thisObj.element.trigger('limitNotHit', [true]);
thisObj.element.trigger('removeSelection',[templateData]);
}
一切都很好,而且执行起来也很好。然而,在我的所有代码运行之后,我得到一个异常:
“对象不支持属性或方法'getAttribute'”
所以我有一个循环调用该函数 X 次,但它只运行该函数一次以完成。
编辑:循环触发运行该函数的元素上的单击。如果我使用firebug手动触发点击,它仍然会抛出异常
编辑2:在jquery-1.6.2.js中的第4550行抛出异常
执行 lineItem[0].parentNode.removeChild( lineItem[0] );
也会导致延迟异常。看起来 @zzzz 关于一些奇怪的缓存清理的说法是正确的。
这只发生在使用 .trigger('click') 时,而不是实际单击元素时
最佳答案
我相信这是因为 IE 将单击事件冒泡到不再存在的节点(然后 jQuery 尝试对此执行某些操作?)。
我假设因为您的点击删除了父元素,所以当它尝试冒泡到该元素时它会中断。
无论如何,当使用backbone.js重新渲染更改事件时,我在IE中看到了完全相同的事情 - 更改事件冒泡,但节点不再存在(因为它被再次渲染,所以它是一个新节点)并且它引发此错误。
使用 event.stopPropagation [ http://api.jquery.com/event.stopPropagation/ ] 过去曾努力为我解决这个问题,请尝试一下。
关于jquery - 为什么 jQuery remove 在 IE8 中抛出 .attr 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7114368/