【问题标题】:Uncaught TypeError: Cannot read property 'documentElement' of null未捕获的类型错误:无法读取 null 的属性“documentElement”
【发布时间】:2015-02-13 11:15:52
【问题描述】:

在我包含 bootstrap.js 之后

<script type="text/javascript" src="/js/bootstrap/js/bootstrap.js"></script>

我在控制台中收到以下错误: 未捕获的类型错误:无法读取 null 的属性“documentElement”

靴子折叠工作正常,但控制台收到垃圾邮件此错误。 我在引导之前包含了 jquery。

以前有人遇到过这个问题吗?

编辑:

  Tooltip.prototype.show = function () {
var e = $.Event('show.bs.' + this.type)

if (this.hasContent() && this.enabled) {
  this.$element.trigger(e)

  var inDom = $.contains(this.$element[0].ownerDocument.documentElement, this.$element[0])
  if (e.isDefaultPrevented() || !inDom) return
  var that = this

这是来自 bootstrap.js 脚本的片段。 似乎错误总是出现在 documentElement 部分的 var inDom 行中的工具提示函数中

【问题讨论】:

  • 错误指向哪一行代码?
  • 能否添加一个现场演示?

标签: jquery twitter-bootstrap collapse


【解决方案1】:

您很可能还有另一个工具提示库仍在使用中(如 JQueryUI/Tooltip),或者您正尝试将工具提示直接附加到文档元素。请查看您的代码以了解以下内容:

$(document).tooltip

或类似的东西,并将其删除以使事情再次正常工作。请务必查看您可能已经拥有的所有其他 .tooltip() 调用,因为它们很可能不再工作:您需要手动将它们移植到 Bootstrap 以使其再次工作.

如果您想同时保留 Bootstrap/TooltipJQueryUI/Tooltip(假设这是您的场景),您可以使用 $.widget.bridge 手动更改 JQueryUI 工具提示插件名称:

// Change JQueryUI/tooltip plugin name to 'uitooltip' to fix name collision with Bootstrap/tooltip
$.widget.bridge('uitooltip', $.ui.tooltip);

您需要在导入 JQueryUI js 文件之后并在导入 Bootstrap js 文件之前执行此操作:当您使用它时,我还建议您对 button/uibutton 执行相同的操作来解决完全相同的问题。结果代码将如下所示:

<script type="text/javascript" src="path/to/jqueryui.js" />
<script type="text/javascript">
// Change JQueryUI plugin names to fix name collision with Bootstrap:
$.widget.bridge('uitooltip', $.ui.tooltip);
$.widget.bridge('uibutton', $.ui.button);
</script>
<script type="text/javascript" src="path/to/bootstrap.js" />

然后您可以将您的 $(document).tooltip 调用重新路由到 $(document).uitooltip 以使一切正常。

或者,如果您没有找到有问题的 js 代码和/或您不想使用 $.widget.brige,您可以随时手动修补 bootstrap.js 以避免这种情况的错误。这可以通过替换以下行来完成(bs 3.3.1 中的#1384):

var inDom = $.contains(this.$element[0].ownerDocument.documentElement, this.$element[0])

用这一行:

var inDom = $.contains((this.$element[0].ownerDocument || this.$element[0]).documentElement, this.$element[0])

请记住,您的代码中的某个地方仍然会出现损坏的 .tooltip() 调用。

另见:

http://www.ryadel.com/2015/01/03/using-jquery-ui-bootstrap-togheter-web-page/(我在博客上写的一篇文章以更好地解释这个问题)

https://github.com/twbs/bootstrap/issues/14483

http://jqueryui.com/tooltip/

【讨论】:

  • 我确切地知道是什么破坏了我的引导设置,您为我节省了至少一个小时的狩猎时间,非常感谢您的回答。
【解决方案2】:

在初始化之前检查元素是否为空

if($('[data-toggle="tooltip"]') != null)
{
    $('[data-toggle="tooltip"]').tooltip();    
}

【讨论】:

    猜你喜欢
    • 2016-11-30
    • 2021-08-31
    • 1970-01-01
    • 2015-06-18
    • 2021-11-18
    • 1970-01-01
    • 2022-01-19
    • 2021-12-01
    相关资源
    最近更新 更多