Internet Explorer 中的 jQuery“对象不支持此属性或方法”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1422629/
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
jQuery "Object doesn't support this property or method" in Internet Explorer
提问by B. Slavin
I am using jQuery to add some dynamic content to a website.
我正在使用 jQuery 向网站添加一些动态内容。
I am attempting to create a new DOM element on-demand using the following code:
我正在尝试使用以下代码按需创建新的 DOM 元素:
container = $('<div id="myContainer"></div>');
This works perfectly in Firefox and Safari, but Internet Explorer is generating an error. The IE error is: Object doesn't support this property or method
这在 Firefox 和 Safari 中完美运行,但 Internet Explorer 生成错误。IE错误是:Object doesn't support this property or method
I know that jQuery is loading properly, and have tried both the jQuery
and $
syntax.
我知道 jQuery 正在正确加载,并且已经尝试了jQuery
和$
语法。
Any ideas as to what might be causing this?
关于可能导致这种情况的任何想法?
采纳答案by inkedmn
If you want to add a DOM element, the code needs to be modified a bit:
如果要添加DOM元素,代码需要稍微修改一下:
$('body').append('<div id="myContainer"></div>');
// body can be whatever containing element you want to hold myContainer
$('#myContainer').html('whatever you want inside of myContainer');
回答by Mighub
I don't know if it can help but I fixed my problem. Basically IE doesn't want to assign jquery object to an undefined variable.
我不知道它是否有帮助,但我解决了我的问题。基本上 IE 不想将 jquery 对象分配给未定义的变量。
So what I did is declare this a local variable instead.
所以我所做的是将其声明为局部变量。
Before:
前:
function foo() {
bar = $('#bar');
}
After:
后:
function foo() {
var bar = $('#bar');
}
回答by Andy Adiwidjaja
I had a similar problem. Internet explorer throws this error when you try to modify a global symbol. This is the case not only for reserved words. My example was:
我有一个类似的问题。当您尝试修改全局符号时,Internet Explorer 会引发此错误。这不仅适用于保留字。我的例子是:
function foo() {
iframe = $("myDiv").append("<iframe></iframe>");
}
This solves it:
这解决了它:
function foo() {
var iframe = $("myDiv").append("<iframe></iframe>");
}
This, too:
这个也是:
function foo() {
myIframe = $("myDiv").append("<iframe></iframe>");
}
(but the first is better style anyway)
(但无论如何第一个是更好的风格)