javascript - Jquery $(文档).prepend : "TypeError: e is null" inside delegated event handler?

标签 javascript jquery

这条简单的线:

$(document).prepend('<div>some text</div>');

从 Jquery 库中产生一个错误:

TypeError: e is null

错误来自 Jquery 文件,而不是我的代码。我的函数不使用任何变量“e”。 Jquery 版本是 1.9.1。我没有发现任何导致此错误的已知问题,但上面的行是导致该错误的原因,因为注释掉该行时不会发生错误。我可以在其他地方追加元素就好了。从 MouseEnter 事件处理程序调用出现问题行的函数,如果这有区别的话。

知道是什么原因造成的吗?

如果您需要更多代码(为清楚和简洁起见,省略了不相关的行):

// Create a delegate event handler:
this.protected.$element.on('mouseenter','.'+this.protected.Prefrences.TriggerClass,function(e){base.eMouseEnter(e,this)});


// Called when the user hovers over the tooltip
ATK.ToolTip.prototype.eMouseEnter = function(e, eventThis){
   this.ShowToolTip($(eventThis));   
}


ATK.ToolTip.prototype.ShowToolTip = function($TipElement){
   $(document).prepend('<div>sometext</div>'); // Triggers "TypeError: e is null" ?!
}

最佳答案

您正在尝试将 div 元素添加到 document 的开头,而不是:

$(document).prepend('<div>sometext</div>');

尝试做:

$(document.body).prepend('<div>sometext</div>');

关于javascript - Jquery $(文档).prepend : "TypeError: e is null" inside delegated event handler?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22551634/

相关文章:

javascript - 为什么没有分号的代码在 JavaScript 中仍然可以工作?

javascript - 如何使用 JavaScript 添加/删除表格行?

javascript - 在 ABAP 中加密字符串并在 JavaScript 中解密

javascript - 语法错误 : unexpected end of input parseJSON

javascript - 如何防止变量在javascript中改变

javascript - 如何在 JavaScript 中存储图像和视频文件列表?

JQuery DataTables - 搜索不起作用,sData 为空错误

javascript - 2 列列表行 CSS JS 上的高度相同

javascript - 自定义或可重用的 HTML 组件

javascript - Angular 7 迭代表