为什么$(document).append()在jQuery 1.9.1中不起作用? [英] Why $(document).append() doesn't work in jQuery 1.9.1?

查看:112
本文介绍了为什么$(document).append()在jQuery 1.9.1中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么从jQuery 1.9.1开始,以下代码段不起作用?以前的版本工作正常。

Why following piece of code doesn't work since jQuery 1.9.1? With previous versions works fine.

$(function () { 
    $(document).append(test);
    document.write('done');
});
var test = {
    version: "1.0",
};

JSFiddle: http://jsfiddle.net/Chessjan/NsjqM/

JSFiddle: http://jsfiddle.net/Chessjan/NsjqM/

在JS控制台中,它会发出如下错误:

In JS console it issues error like this:

TypeError: document is null
safeFrag = document.createDocumentFragment(); jquery-1.9.1.js (line 5823)

编辑:

感谢大家快速而广泛的回答。观察到的问题是偶然发现的,当然, $(document.body).append()是正确的方法。

Thanks everybody for quick and extensive aswers. Observed issue was found by accident, and of course, $(document.body).append() is proper approach.

推荐答案

jQuery 1.9.x调用

jQuery 1.9.x calls

this[ 0 ].ownerDocument

在其 buildFragment()方法中。由于您传入了文档,因此调用

within its buildFragment() method. Since you pass in the document, the call

document.ownerDocument

将引用 null 并导致错误。任何其他节点都将引用文档,这当然有效。

will reference to null and cause the error. Any other node will reference the document, which of course, works.

结论:不要调用 $(document).append()但是要使用 $(document.body)例如。

Conclusion: Don't call $(document).append() but use $(document.body) for instance.

这篇关于为什么$(document).append()在jQuery 1.9.1中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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