javascript - jquery: "Exception thrown and not caught"在 IE8 中,但在其他浏览器中有效

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

我的代码在其他浏览器中运行良好,但在 IE8 中我收到“页面错误”——当我点击它时它说: “抛出异常但未捕获行:16 字符:15120 代码:0 网址: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);
});

};

最佳答案

代码在 IE8 中能正常工作还是会出错?我问的原因是因为如果它按预期工作,你可以将它包装在 try{ } catch{\\do nothing } block 中并将其归结为另一个 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 方法用于向 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 对象并将其附加到该对象,然后将其附加到您随后附加到 facebookfeed div 的 html 对象。令人困惑吧?

关于javascript - jquery: "Exception thrown and not caught"在 IE8 中,但在其他浏览器中有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6371649/

相关文章:

javascript - 删除 Angular 表中的按钮

javascript - jQuery.data() 存储在哪里?

javascript - jquery onclick 模态弹出窗口中的按钮更改当前 td 的值

php - 在 Ajax 提交后强制下载 TCPDF 创建的 PDF

javascript - 通过 Blogger API v3.0 为帖子插入评论

ios - Swift 3 过滤 Json 和重新排序

Javascript 对象部分数组 if then 语句

javascript - 使用 jQuery 发送 JSON 数据

javascript - 检测价格是否高于或低于 200.00,然后设置(此)表格行的样式

php - 从一种 json 格式转换为另一种格式