Javascript 未捕获的类型错误:无法读取未定义的属性“createDocumentFragment”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28177917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:20:02  来源:igfitidea点击:

Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined

javascriptjquerytwitter-bootstrap

提问by user61629

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

我正在尝试抓取网页并加载到引导程序 2.3.2 弹出窗口中。到目前为止,我有:

$.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:

当我激活此代码时,出现错误:

Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined

未捕获的类型错误:无法读取未定义的属性“createDocumentFragment”

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

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

jsfiddle

jsfiddle

回答by t.niese

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

错误的原因是$(this).html($link);在您的.done()回调中。

thisin 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在回调中是指[...]object that represents the ajax settings used in the call ($.ajaxSettings merged with the settings passed to $.ajax)[...]而不是指$(".btn.btn-navbar")(或者任何你期望它应该指的地方)。

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

抛出错误是因为 jQuery 会在您执行时.createDocumentFragment()在内部调用ownerDocument您传递的对象,但在您的代码中,它不是 DOMElement,并且没有. 正因为如此是和多数民众之所以被称为上。this$(this).html($link);thisownerDocumentownerDocumentundefinedcreateDocumentFragmentundefined

You either need to use the contextoption for your ajaxrequest. Or you need to save a referenceto the DOMElement you want to change in a variable that you can access in the callback.

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

回答by Kevin Felisilda

That error occur because thisis referring to the ajax object and not on the DOM element, to solve this you can do something like this:

发生该错误this是因为指的是 ajax 对象而不是 DOM 元素,要解决此问题,您可以执行以下操作:

$('form').on('submit', function(){
    var thisForm = this;

    $.ajax({
        url: 'www.example.com',
        data: {}
    }).done(function(result){
        var _html = '<p class="message">' + result + '</p>';
        $(thisForm).find('#resultDiv').html(_html);
    });
});