未捕获的TypeError:无法读取未定义的属性'createDocumentFragment' [英] Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined

查看:641
本文介绍了未捕获的TypeError:无法读取未定义的属性'createDocumentFragment'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取一个网页并将其加载到Bootstrap 2.3.2弹出窗口中.到目前为止,我有:

I am trying to grab a webpage and load into a bootstrap 2.3.2 popover. So far I have:

$.ajax({
  type: "POST",
  url: "AjaxUpdate/getHtml",
  data: {
    u: 'http://stackoverflow.com'
  },
  dataType: 'html',
  error: function(jqXHR, textStatus, errorThrown) {
    console.log('error');
    console.log(jqXHR, textStatus, errorThrown);
  }
}).done(function(html) {
    console.log(' here is the html ' + html);

    $link = $('<a href="myreference.html" data-html="true" data-bind="popover"' 
            + ' data-content="' + html + '">');
    console.log('$link', $link);
    $(this).html($link);

    // Trigger the popover to open
    $link = $(this).find('a');
    $link.popover("show");

当我激活此代码时,我得到错误消息:

When I activate this code I get the error:

未捕获的TypeError:无法读取未定义的属性'createDocumentFragment'

Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined

这是什么问题,我该如何解决?

What is the problem here and how can I fix it?

jsfiddle

推荐答案

发生错误的原因是.done()回调中的$(this).html($link);.

The reason for the error is the $(this).html($link); in your .done() callback.

this指的是[...]object that represents the ajax settings used in the call ($.ajaxSettings merged with the settings passed to $.ajax)[...]而不是$(".btn.btn-navbar")(或者您期望它指代的位置).

this in the callback refers to the [...]object that represents the ajax settings used in the call ($.ajaxSettings merged with the settings passed to $.ajax)[...] and not to the $(".btn.btn-navbar") (Or whatever you expect where it should refer to).

引发错误是因为当执行$(this).html($link);时,jQuery会内部调用this传递的对象的ownerDocument内部调用.createDocumentFragment(),但是在代码中,this不是DOMElement,并且不会有一个ownerDocument.因为ownerDocumentundefined,这就是为什么在undefined上调用createDocumentFragment的原因.

The error is thrown because jQuery will internally call .createDocumentFragment() on the ownerDocument of object you pass with this when you execute $(this).html($link); but in your code the this is not a DOMElement, and does not have a ownerDocument. Because of that ownerDocument is undefined and thats the reason why createDocumentFragment is called on undefined.

您要么需要对ajax请求使用 context 选项.或者,您需要在要在回调中访问的变量中保存要更改的DOMElement的引用.

You either need to use the context option for your ajax request. Or you need to save a reference to the DOMElement you want to change in a variable that you can access in the callback.

这篇关于未捕获的TypeError:无法读取未定义的属性'createDocumentFragment'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆