【问题标题】:jquery: "Exception thrown and not caught" in IE8, but works in other browsersjquery:在 IE8 中“抛出异常但未捕获”,但在其他浏览器中有效
【发布时间】:2011-09-16 07:56:58
【问题描述】:

我的代码在其他浏览器中运行良好,但在 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);
});

};

【问题讨论】:

  • 您的结束 div 在 div 之后和 > 之前实际上没有空格,对吗?我认为这不是问题,但是...
  • 谢谢 - 纠正了他们,但正如你所怀疑的 - 问题仍然存在

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


【解决方案1】:

代码是在 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

因此,如您在 cmets 中提到的那样,要围绕 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。很迷惑吧?!

【讨论】:

  • 谢谢 csharpsi - 你找到了问题。
  • 单引号和双引号一定是问题所在。
  • 我仍在努力使新代码正确(我什至不知道 DIV 可能会自动关闭。如果我没搞定,我可以回来找你更正吗?给我吗?
  • 当然可以。 Div 不是自动关闭的,但 JQuery 执行一些魔术来根据需要创建 DOM 元素。我认为您可以使用 $('
    ') 但我之前使用的方法会提供相同的结果
  • 如果您认为它解决了您的问题,请您标记为已回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多