简体   繁体   English

为什么$(document).append()在jQuery 1.9.1中不起作用?

[英]Why $(document).append() doesn't work in jQuery 1.9.1?

Why following piece of code doesn't work since jQuery 1.9.1? 为什么从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/

In JS console it issues error like this: 在JS控制台中,它会发出如下错误:

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

Edit: 编辑:

Thanks everybody for quick and extensive aswers. 谢谢大家快速和广泛的as。 Observed issue was found by accident, and of course, $(document.body).append() is proper approach. 观察到的问题是偶然发现的,当然, $(document.body).append()是正确的方法。

jQuery 1.9.x calls jQuery 1.9.x调用

this[ 0 ].ownerDocument

within its buildFragment() method. 在其buildFragment()方法中。 Since you pass in the document , the call 由于您传入了document ,因此调用

document.ownerDocument

will reference to null and cause the error. 将引用null并导致错误。 Any other node will reference the document , which of course, works. 任何其他节点都将引用该document ,这当然是有效的。


Conclusion: Don't call $(document).append() but use $(document.body) for instance. 结论:不要调用$(document).append()而是使用$(document.body)

Your code will of never worked. 你的代码永远不会奏效。 It has to document.body not document . 它必须是document.body 而不是 document

Here's a few examples in different versions of it not working: 以下是不同版本中的一些示例:

jQuery 1.6.4: http://jsfiddle.net/us9Kz/ jQuery 1.6.4: http//jsfiddle.net/us9Kz/
jQuery 1.7.2: http://jsfiddle.net/us9Kz/1/ jQuery 1.7.2: http//jsfiddle.net/us9Kz/1/
jQuery 1.8.3: http://jsfiddle.net/us9Kz/3/ jQuery 1.8.3: http//jsfiddle.net/us9Kz/3/
jQuery 1.9.1: http://jsfiddle.net/us9Kz/4/ jQuery 1.9.1: http//jsfiddle.net/us9Kz/4/
jQuery 2.0.0b1: http://jsfiddle.net/us9Kz/5/ jQuery 2.0.0b1: http//jsfiddle.net/us9Kz/5/

Code working with document.body (on jQuery 1.9.1): http://jsfiddle.net/us9Kz/6/ 代码使用document.body (在jQuery 1.9.1上): http//jsfiddle.net/us9Kz/6/

To answer your question i tried in JSfiddle all the available jQuery versions. 为了回答你的问题,我在JSfiddle中尝试了所有可用的jQuery版本。 It happened to give the same error. 碰巧给出了同样的错误。

Why it doesnt work: document becomes something like [object HTMLDocument] when cast to string, and there is of course no such id, it will return null. 为什么它不起作用:当转换为字符串时,文档变成类似[object HTMLDocument]的东西,当然没有这样的id,它将返回null。

The following works: 以下作品:

var test = "1.0"
$('body').append(test);

or doing it trough object notation like you did: 或者通过像你这样的对象符号来做:

var test = {
    version: '1.0'
}
$('body').append(test.version)

Inside the jQuery code it has this line: 在jQuery代码中,它有这一行:

jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, this );

this is the jQuery object you selected. this是您选择的jQuery对象。 In your case, the document. 在你的情况下,文件。 The ownerDocument value of document is null and this is what is passed through as document to the call to document.createDocumentFragment(); document的ownerDocument值为null,这是作为document传递给document.createDocumentFragment(); . Hence you get the error that document is null (Slightly bad naming of variables there as it makes you think the document object itself is somehow null) 因此,您得到document为null的错误(变量的命名略有错误,因为它使您认为文档对象本身在某种程度上为null)

As other people have said. 正如其他人所说的那样。 Append to the body instead and it will work fine. 附加到身体,它会工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM