未捕获的TypeError:无法读取null的属性'documentElement'

the*_*now 5 jquery collapse twitter-bootstrap

在我包含bootstrap.js之后

<script type="text/javascript" src="/js/bootstrap/js/bootstrap.js"></script>
Run Code Online (Sandbox Code Playgroud)

我在控制台中遇到以下错误: 未捕获TypeError:无法读取null的属性'documentElement'

靴子崩溃工作正常,但控制台发送垃圾邮件这个错误.我在bootstrap之前包含了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
Run Code Online (Sandbox Code Playgroud)

这是来自bootstrap.js脚本的snipet.似乎错误始终出现在documentElement部分的var inDom行中的tooltip函数中

Dar*_*eal 19

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

$(document).tooltip
Run Code Online (Sandbox Code Playgroud)

或类似的东西,并删除它,以使事情再次发挥作用.请务必查看您可能已经拥有的所有其他.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);
Run Code Online (Sandbox Code Playgroud)

你需要在导入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" />
Run Code Online (Sandbox Code Playgroud)

然后你可以将$(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])
Run Code Online (Sandbox Code Playgroud)

用这一行:

var inDom = $.contains((this.$element[0].ownerDocument || this.$element[0]).documentElement, this.$element[0])
Run Code Online (Sandbox Code Playgroud)

请记住,你的代码中的某个地方仍然会有一个破坏的.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/