javascript jquery:IE8 中的“异常抛出但未捕获”,但适用于其他浏览器

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

jquery: "Exception thrown and not caught" in IE8, but works in other browsers

javascriptjqueryjsoninternet-explorer-8cross-browser

提问by user801347

My code works fine in other browsers, but in IE8 I get "error on page" - and when I click that it says: "Exception thrown and not caught Line: 16 Char: 15120 Code: 0 URI: http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"

我的代码在其他浏览器中运行良好,但在 IE8 中我得到“页面错误” - 当我点击它时,它说:“抛出异常但未捕获行:16 字符:15120 代码:0 URI:http://ajax。 googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js

I tried linking to jquery.js (rather than jquery.min.js) and to 1.5.1/jquery.min.js, but problem still remains.

我尝试链接到 jquery.js(而不是 jquery.min.js)和 1.5.1/jquery.min.js,但问题仍然存在。

Can someone correct/improve my code for me, or guide me as to where to look. Thanks

有人可以为我更正/改进我的代码,或指导我去哪里寻找。谢谢

<script type="text/javascript">
function fbFetch() 
{
var token = "<<tag_removed>>&expires_in=0";

//Set Url of JSON data from the facebook graph api. make sure callback is set with a '?' to overcome the cross domain problems with JSON
var url = "https://graph.facebook.com/<<ID_REMOVED>>?&callback=?&access_token=" + token;

//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
$.getJSON(url, function(json)
{
    json.data = json.data.reverse();  // need to reverse it as FB outputs it as earliest last!
    var html = "<div class='facebook'>";

    //loop through and within data array's retrieve the message variable.
    $.each(json.data, function(i, fb)
    {
        html += "<div class='n' >" + fb.name;           
        html += "<div class='t'>" + (dateFormat(fb.start_time, "ddd, mmm dS, yyyy")) + " at " + (dateFormat(fb.start_time, "h:MMtt")) + "</div >"; 
        html += "<div class='l'>" + fb.location + "</div >"; 
        html += '<div class="i"><a target="_blank" title="opens in NEW window" href="https://www.facebook.com/pages/<<id_removed>>#!/event.php?eid=' + fb.id + '" >more info...</a></div>'; 
        html += "</div >";              
    }
    );
    html += "</div>";

    //A little animation once fetched
    $('.facebookfeed').animate({opacity: 0}, 500, function(){
        $('.facebookfeed').html(html);
    });
    $('.facebookfeed').animate({opacity: 1}, 500);
});

};

};

采纳答案by simonlchilds

Does the code do the job in IE8 or does it break? The reason I ask is because if it works as expected you could just wrap it in a try{ } catch{ \\do nothing }block and put it down to another thing IE is rubbish at.

代码在 IE8 中完成工作还是中断?我问的原因是因为如果它按预期工作,您可以将它包装在一个try{ } catch{ \\do nothing }块中并将其归结为 IE 垃圾的另一件事。

You may be better off creating an object for the creation of the facebook div. Something like...

您最好为创建 facebook div 创建一个对象。就像是...

var html = $('<div />');
html.attr('class', 'facebook');

Then in your each loop you can do this...

然后在你的每个循环中你可以这样做......

$('<div />').attr('class', 'n').append(fb.name).appendTo(html);
$('<div />').attr('class', 't').append etc...

Then append htmlto the facebookfeedobject

然后追加htmlfacebookfeed对象

Doing this may remove the scope for error when using single quotes and double quotes when joining strings together, which in turn may solve your issue in IE8

在将字符串连接在一起时使用单引号和双引号时,这样做可能会消除错误的范围,这反过来可能会解决您在 IE8 中的问题

$('.facebookfeed').fadeOut(500, function(){
      $(this).append(html).fadeIn(500);
});

Hope this helps!

希望这可以帮助!

UPDATE

更新

The append method is used to add stuffto a jquery object. For more info see here

append 方法用于向jquery 对象添加内容。有关更多信息,请参见此处

So to surround the div's as you mentioned in the comments you would do something like this...

所以要像你在评论中提到的那样围绕 div,你会做这样的事情......

var nDiv = $('<div />').attr('class', 'n').append(fb.name);
$('<div />').attr('class', 't').append(fb.somethingElse).appendTo(nDiv);
// etc

And then you would need to append that to the html div like so...

然后你需要像这样将它附加到 html div...

html.append(nDiv);

So that would give you

所以这会给你

<div class="facebook">
     <div class="n">
         value of fb.name
         <div class="t">
              value of fb.somethingElse
         </div>
     </div>
</div>

So what you have done is created a new jquery object and appended to that, then appended that to the html object which you have then appended to the facebookfeed div. Confusing huh?!

所以你所做的是创建一个新的 jquery 对象并附加到它,然后将它附加到你已经附加到 facebookfeed div 的 html 对象。令人困惑吧?!