仅在 IE8 中使用 jQuery 的“预期对象”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18020471/
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
"Object expected" error using jQuery only in IE8
提问by victorf
I'm getting an error only in IE (v8, I don't know if it happens in older IE versions, but it does not occur in Chrome or Firefox) which brings me the following message when I use IE dev tool's debbuger:
我只在 IE 中遇到错误(v8,我不知道它是否发生在较旧的 IE 版本中,但它不会发生在 Chrome 或 Firefox 中),当我使用 IE 开发工具的调试器时,它会给我带来以下消息:
Breaking on JSScript runtime error - Object Expected
Here is my affected code:
这是我受影响的代码:
$('#deviceProfileSelection').change(function() { //affected line!!!!
// rest of my code...
});
This element #deviceProfileSelection
is defined as the following:
该元素#deviceProfileSelection
定义如下:
<select id="deviceProfileSelection">
<option value=""><?php echo getSysMessage("dropDownSelect")?></option>
<!-- and other values...-->
</select>
I've already tried defining the .change listener into a $(document).ready(function () {});
but no success at all. Any other idea?
我已经尝试将 .change 侦听器定义为 a$(document).ready(function () {});
但根本没有成功。还有其他想法吗?
EDIT
编辑
I was trying to include a div using a PHP decision structure, where if a condition was true, it should print a div. But, actually it wasn't printing, I mean, it wasn't printing the opening tag 'div', only the closing tag 'div'.
我试图包含一个使用 PHP 决策结构的 div,如果条件为真,它应该打印一个 div。但是,实际上它没有打印,我的意思是,它没有打印开始标签“div”,只打印结束标签“div”。
The browsers could interpret this error, but IE8, and this IE inability was causing the issue.
浏览器可以解释这个错误,但是 IE8 和这个 IE 的无能导致了这个问题。
采纳答案by JLuiz
The problem is that you haven't added jQuery into your code at all.
Add the following inside the <head>
tag:
问题是您根本没有将 jQuery 添加到您的代码中。在<head>
标签内添加以下内容:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
That should fix your problem.
那应该可以解决您的问题。
回答by Levi Hackwith
Check the following:
检查以下内容:
- Is jQuery included when this code is executed? (I'd do an alert on
$
and see what you get) - Make sure the
deviceProfileSection
element exists.
- 执行此代码时是否包含 jQuery?(我会发出警报
$
,看看你会得到什么) - 确保该
deviceProfileSection
元素存在。
回答by seridis
I had a similar issue to this before with IE8, if it's the same issue, it can't find the jquery library, check your path or test it with a cdn
我之前在 IE8 上也遇到过类似的问题,如果是同样的问题,则找不到 jquery 库,请检查您的路径或使用 cdn 进行测试
回答by Tim S
I tried the following code in IE8, and it works just as expected. As suggested in other answers, I used the latest version of JQuery that is compatible with IE8 from the JQuery CDN. Don't use 2.x -- it does not work with older browsers. Also, make sure your change event code is located in a document.ready handler.
我在 IE8 中尝试了以下代码,它按预期工作。正如其他答案中所建议的那样,我使用了 JQuery CDN 中与 IE8 兼容的最新版本的 JQuery。不要使用 2.x - 它不适用于旧浏览器。此外,请确保您的更改事件代码位于 document.ready 处理程序中。
<html>
<head>
<title>IE8 Object Expected</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(function(){
$("#deviceProfileSelection").change(function(){
console.log("#deviceProfileSelection changed to " + $(this).find(":selected").text());
});
});
</script>
</head>
<body>
<form>
<select id="deviceProfileSelection">
<option value="">Select...</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</form>
</body>
</html>
回答by Divya kukar
Just so that no one repeats my mistake, since you are using jquery from an external website( src="http://code.jquery.com/jquery-1.10.2.min.js"), make sure your internet is working
只是为了没有人重复我的错误,因为您使用的是来自外部网站的 jquery(src=" http://code.jquery.com/jquery-1.10.2.min.js"),请确保您的互联网正常工作