jquery:IE8中的“异常抛出但未被捕获”,但在其他浏览器中有效

时间:2011-06-16 12:18:03

标签: javascript jquery json internet-explorer-8 cross-browser

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

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

有人可以为我纠正/改进我的代码,或指导我去哪里看。感谢

<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);
});

};

1 个答案:

答案 0 :(得分:4)

代码是在IE8中完成工作还是破坏了?我问的原因是因为如果它按预期工作,你可以将它包装在try{ } catch{ \\do nothing }块中并将其放到另一个IE垃圾处理器上。

你可能最好创建一个用于创建facebook div的对象。有点像...

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

然后在你的每个循环中你可以做到这一点......

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

然后将html追加到facebookfeed对象

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

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

希望这有帮助!

<强>更新

append方法用于将 stuff 添加到jquery对象。有关详细信息,请参阅here

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

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

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

html.append(nDiv);

这样会给你

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

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

相关问题