Javascript 对象预期 IE8 JS/JQuery 问题 IE8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10524243/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:42:29  来源:igfitidea点击:

Object Expected IE8 JS/JQuery issue IE8

javascriptjqueryinternet-explorer

提问by ?????

This is my first post on here though I browse through SO a lot for answers. I'm running into a problem where IE8 will keep throwing "Object Expected" error. I used IE8's Developer tools and it points to "mymh.js" file

这是我在这里的第一篇文章,尽管我浏览了很多答案。我遇到了一个问题,IE8 会不断抛出“预期对象”错误。我使用了 IE8 的开发工具,它指向“mymh.js”文件

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="/MyMHome/javascript/mymh.js"></script> 

mymh.js file only has the following code

mymh.js 文件只有以下代码

    $(document).ready(function() {          

        $('#hNumber').focus();

        $('#ddlDir').change(function () {

            var selVal = $('#ddlDir').val();

             if (selVal == 'N' || selVal == 'S' || selVal == 'E' || selVal == 'W'){

             $.getJSON('/MyMHome/DimeServlet?strDir='+$('#ddlDir option:selected').val(), function(data) {

                    $('#ddlSt')
                    .find('option')
                    .remove()
                    .end()

                $.each(data, function(i,field){
                    var name = field;
                    $('#ddlSt')

                    .append('<option value= ' + '"' + name + '"' + '>' + name + '</option>');   
                    });
                });

                $('#ddlSt').focus();    
             }else{ 

                    $('#ddlSt')
                    .find('option')
                    .remove()
                    .end()
                    .append('<OPTION selected value="">Choose a direction first</OPTION>');

                }                   
        })
        .trigger('change');             

        $('#reset').click(function(){
             $('#ddlSt')
            .find('option')
            .remove()
            .end()
            .append('<OPTION selected value="">Choose a direction first</OPTION>'); 
             $('#hNumber').focus();                
        });

        $('#hNumber').bind('keyup', function() {
            if($('#hNumber').val().length == 5){
                    $('#ddlDir').focus();
            }
        });             

        $('#submitQuery').click(function(){
            var houseNumber = $('#hNumber').val();
            if(houseNumber.replace(/\s+/g, '').length == 0){
                alert('Please enter a house number.');
                $('#hNumber').focus();  
                return false;
            }else if( (!$.isNumeric(houseNumber)) || houseNumber.indexOf('-') > -1 || houseNumber.indexOf('.') > -1){
                alert('Please enter numbers only. You will be prompted later, if the address requires a suffix.');
                $('#hNumber').focus();  
                return false;
            }else if(houseNumber < 100 || houseNumber > 12999){
                alert('Please enter a house number between 100 and 12999');
                $('#hNumber').focus();
                return false;
            }else if($('#ddlDir option:selected').val() == 'none'){
                alert('Please select a street direction.');
                $('#ddlDir').focus();
                return false;
            }       
        });         

        $('form').keypress(function(e) {
              if (e.keyCode == '13') {
                 e.preventDefault();
                 if($('#ddlSt').is(":focus")){
                     $('#submitQuery').trigger('click');
                 }
                 else{
                     return false;
                 }
               }
        });
});

The error points to the <script ... mymh.js></script>but in the debugger it points to $document.ready(function() {

错误指向<script ... mymh.js></script>但在调试器中它指向$document.ready(function() {

Anyone see anything wrong as to why IE8 would keep throwing that error?

任何人都认为为什么 IE8 会不断抛出该错误?

回答by Dan W

Try placing a semicolon at the end of line 16

尝试在第 16 行的末尾放置一个分号

from

$('#ddlSt')
.find('option')
.remove()
.end()

$.each(data, function(i,field){

to

$('#ddlSt')
.find('option')
.remove()
.end();

$.each(data, function(i,field){

回答by srbhattarai

$(document).ready(function() { });  

This statement may throw Object Expected error because of following reasons:

由于以下原因,此语句可能会抛出 Object Expected 错误:

First, If our link to external javascript file (Eg. jquery-1.8.2.min.js) is not correct. Make sure that your link is at the right path. I have kept my file inside "js" folder and gave the path as:

首先,如果我们指向外部 javascript 文件(例如 jquery-1.8.2.min.js)的链接不正确。确保您的链接位于正确的路径上。我将文件保存在“js”文件夹中,并将路径指定为:

<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>  

Second, If we write script tag as:

其次,如果我们将脚本标签写为:

<script type="application/javascript"></script>  

instead of

代替

<script type="text/javascript"></script> 

回答by Paul Croarkin

I had this problem when using jQuery 2.0.0 in IE8. According to the jQuery site "jQuery 2.x does not support Internet Explorer 6, 7, or 8". Replacing jQuery 2.0.0 with jQuery 1.10.2 fixed this issue for me.

我在 IE8 中使用 jQuery 2.0.0 时遇到了这个问题。根据 jQuery 站点“jQuery 2.x 不支持 Internet Explorer 6、7 或 8”。用 jQuery 1.10.2 替换 jQuery 2.0.0 为我解决了这个问题。

回答by Jacob

Bit of a old post but it maybe comes in handy for people passing by, I had the same problem, i removed the defer attribute in the script tag.

有点旧的帖子,但它可能对路过的人派上用场,我遇到了同样的问题,我删除了脚本标签中的 defer 属性。

<script type="text/javascript" defer="defer" src="js/libs/jquery-1.8.3.min.js"></script>

into:

进入:

<script type="text/javascript" src="js/libs/jquery-1.8.3.min.js"></script>

That worked for me.

那对我有用。

回答by KenD

This may not be relevant, but I just solved the same kind of issue with IE8 - code that ran fine on IE9 and everything else choked on IE8.

这可能无关紧要,但我刚刚解决了与 IE8 相同的问题 - 代码在 IE9 上运行良好,而其他所有内容在 IE8 上都卡住了。

The answer was as simple as changing

答案就像改变一样简单

 <script type="text/javascript" src="/myjavascript.js"></script> 
 <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>

to

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="/myjavascript.js"></script> 

... in other words, JQuery first. This fixed the problem for me; YMMV.

... 换句话说,首先是 JQuery。这为我解决了问题;天啊。