Javascript 为什么 chrome 会在这里抛出“未捕获的错误:NOT_FOUND_ERR:DOM Exception 8”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4624029/
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
Why does chrome throw "Uncaught Error: NOT_FOUND_ERR: DOM Exception 8" here?
提问by Hubro
Below is the code for my YWA wrapper
下面是我的 YWA 包装器的代码
var astr_ywascript = (document.createElement("script").type = "text/javascript").src = "http://d.yimg.com/mi/eu/ywa.js";
document.head.appendChild(astr_ywascript); // <- error on this line
It's run on page load, so it makes no sense that JS can't find the document head tag.
它在页面加载时运行,因此JS找不到文档头标签是没有意义的。
Any ideas?
有任何想法吗?
Thanks
谢谢
Opera throws this error on the same line. Uncaught exception: Error: WRONG_ARGUMENTS_ER
Firebug says: document.head is undefined [Break On This Error] document.head.appendChild(astr_ywascript);
Opera 在同一行抛出这个错误。Uncaught exception: Error: WRONG_ARGUMENTS_ER
萤火虫 说:document.head is undefined [Break On This Error] document.head.appendChild(astr_ywascript);
采纳答案by spender
In the line
在行中
(document.createElement("script").type = "text/javascript").src
you are setting the src property of a string. The assignment in parentheses returns the assigned value. You make the same mistake later in the line, ultimately assigning "http://d.yimg.com/mi/eu/ywa.js"
to astr_ywascript
您正在设置字符串的 src 属性。括号中的赋值返回分配的值。你在后面犯了同样的错误,最终分配"http://d.yimg.com/mi/eu/ywa.js"
给astr_ywascript
Split it out onto separate lines:
将其拆分为单独的行:
var el=document.createElement("script");
el.type="text/javascript"
el.src=...
document.head.appendChild(el);
Raw Javascript rarely behaves in the fluid way one gets used to with jQuery.
原始 Javascript 很少以人们习惯使用 jQuery 的流畅方式运行。
You might also want to get the head as follows:
您可能还想按如下方式获取头部:
document.getElementsByTagName("head")[0]
回答by Jeff Dickey
I got this error with Backbone.js because I was doing:
我在 Backbone.js 上遇到了这个错误,因为我在做:
$('#backbone').html(@view.render())
Instead of
代替
$('#backbone').html(@view.render().el)
Like I should have done. Still don't really know what this 'el' business is, but I'm sure I'll figure it out soon.
就像我应该做的那样。仍然不知道这个“el”业务是什么,但我相信我很快就会弄清楚。
回答by Grallen
Just appending this answer for people who might hit this page like I did while searching for an answer to a “Uncaught Error: NOT_FOUND_ERR: DOM Exception 8
error.
只是为可能会像我在搜索“Uncaught Error: NOT_FOUND_ERR: DOM Exception 8
错误答案时那样点击此页面的人附加此答案。
I had this error in Chrome. In my case I was using .appendChild(ttt);
我在 Chrome 中遇到了这个错误。就我而言,我正在使用.appendChild(ttt);
The issue was that ttt
was an array object instead of the div I intended to append.
问题是这ttt
是一个数组对象,而不是我打算附加的 div。
So make sure what you are appending is actually a DOM object and not accidentally a different object type.
因此,请确保您附加的内容实际上是一个 DOM 对象,而不是意外的不同对象类型。