jQuery“事件”为空或不是对象”使用empty()或html()时出错
我在 IE 中收到以下错误:
“events”为 null 或不是对象 -- jquery-latest.js?d=1848173663,第 113 行字符 467
我正在使用 jQuery 1.4.2,我还无法升级,因为我们使用的是旧版本jQuery UI 的版本,并且使用 1.4.2 以后的版本时存在太多错误。
当我第二次运行这段代码时,出现以下错误:
$.post("page/view.do?undoCache=" + Math.random(), {
pageId: pId
}, function(xmlContent){
console.log('1');//get this one
$('#reloadCenterDiv').empty();
console.log('2');//don't get this one unless line above is commented out, then will run til next line
$('#reloadCenterDiv').html(xmlContent);
console.log('3');//don't get this
});
我很确定在调用之间我没有对 #reloadCenterDiv 执行任何其他操作。
谷歌搜索错误“‘事件’为空或不是对象”我发现了这个:
“听起来像是对某个事件的引用 处理程序仍然在那里,当 处理程序本身已经消失了。”
这听起来合乎逻辑。关于为什么以及何时会发生此错误的任何其他想法?
我已经找到了发生这种情况的位置,但我的所有线索都到此为止。
我如何清理这些东西以便我可以调用空再次在 #reloadCenterDiv 上使用 () 或 html()
这是 #reloadCenterDiv 的 HTML:
<div id="reloadCenterDiv" style="border:none; margin: 0; overflow:auto; overflow-y:scroll; height: auto;"></div>
I am getting the following error in IE:
'events' is null or not an object -- jquery-latest.js?d=1848173663, line 113 character 467
I am using jQuery 1.4.2, I'm not in a position to upgrade yet as we are on an older version of jQuery UI and have way too many bugs using anything newer than 1.4.2.
I get following error when I run this bit of code the second time:
$.post("page/view.do?undoCache=" + Math.random(), {
pageId: pId
}, function(xmlContent){
console.log('1');//get this one
$('#reloadCenterDiv').empty();
console.log('2');//don't get this one unless line above is commented out, then will run til next line
$('#reloadCenterDiv').html(xmlContent);
console.log('3');//don't get this
});
I'm pretty sure I'm not doing anything else to #reloadCenterDiv between calls.
Googling around for the error "'events' is null or not an object" I found this:
"Sounds like a reference to an event
handler is still there, when the
handler itself is already gone."
That sounds logical. Any other ideas of why and when this error would occur?
I have found where this is happening, but all clues for me end there.
How do I clean things up so I can call empty() or html() on #reloadCenterDiv again?
Here is the HTML for #reloadCenterDiv:
<div id="reloadCenterDiv" style="border:none; margin: 0; overflow:auto; overflow-y:scroll; height: auto;"></div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定,但看起来
jQuery.cache
正在被覆盖。由于 DOM 元素(必要时)有一个映射到 jQuery.cache 的序列号,因此当您运行诸如 .empty() 之类的函数时,jQuery 会假定相关数据存在,查找该元素的数据,并将其删除。
代替你的第一个日志,执行以下操作:
看看它给你带来了什么。我敢打赌有什么东西正在覆盖它。也许您加载了 jQuery 两次?
这是一个故意删除
jQuery.cache
的示例。它给出了类似的错误。编辑:
以下评论摘要。在
.empty()
(或者实际上是cleanData()
)期间,jQuery 从所有后代元素中获取 Expando,以便删除关联的数据。问题是,当 jQuery 这样做时,它假设数据已成功定位。在这种情况下,数据以某种方式与元素解除关联,因此使用 Expando 的值检索数据将返回
undefined
。因为 jQuery 没有(或者在 1.4.2 中没有)验证是否找到了数据,所以它尝试访问数据上的
events
属性会导致错误,因为data< /code> 是
未定义
。jQuery 的更新版本使用
if ( data && data.events ) {
修复了这个问题,它验证是否存在某个对象来请求其events
属性。Not sure, but it would seem like
jQuery.cache
is being overwritten.Since a DOM element has (when necessary) a serial number that maps to
jQuery.cache
, when you run a function like.empty()
, jQuery assumes the related data exists, looks up the data for that element, and deletes it.In place of your first log, do this:
And see what it gives you. I'll bet that something is overwriting it. Perhaps you're loading jQuery twice?
Here's an example that intentionally deletes
jQuery.cache
. It gives a similar error.EDIT:
Summary of the comments below. During
.empty()
(or actuallycleanData()
) jQuery grabs the expando from all descendant elements in order to delete the associated data.The issue is that when jQuery does so, it assumes that the data was successfully located. In this case, somehow the data is being disassociated from the element, so retrieving the data using the value of the expando is returning
undefined
.Because jQuery doesn't (or didn't in 1.4.2) verify that data was found, its attempt to access the
events
property on the data is causing an error, because againdata
isundefined
.Updated versions of jQuery fix it with
if ( data && data.events ) {
, which verifies that there is some object against which to ask for itsevents
property.如果您无法更新 jQuery,则可以设置 HTML:
这本质上是做同样的事情。
If you can't update your jQuery, you can set the HTML instead:
This is essentially doing the same thing.