【问题标题】:JavaScript & JQuery not working correctly in IE [closed]JavaScript & JQuery 在 IE 中无法正常工作 [关闭]
【发布时间】:2013-06-20 11:12:01
【问题描述】:

作为 JS 的新手,我很高兴能够在 webkit 浏览器中使用 MaxMind API 的脚本,但是,IE 不想参加聚会(我认为只有 CSS/HTML 问题困扰着我)即!)

我正在测试的页面可以在这里找到 - http://www.ontrackdatarecovery.es/images/phone/130612-es-dynamic-phone-geoip.html

您应该可以看到,它可以在 Chrome 等中运行,但不能在 IE (8) 中运行。非常感谢任何帮助!

谢谢

【问题讨论】:

  • 您的控制台在 IE8 中显示什么?
  • “有效”或“无效”是什么意思?
  • 为什么不在这里发布相关代码?
  • 如果您指出代码的哪一部分导致您出现问题,将会很有帮助。 IE。将代码放入您的问题中。
  • IE8 不太喜欢 switch 语句。试试 if/else。

标签: javascript jquery internet-explorer-8 maxmind


【解决方案1】:

控制台显示'return' statement outside of function。 尝试将 return 语句更改为 break

default:
    regionPhone = '<p>This is an IP from ' + userCountry + '</p>';
    break;

【讨论】:

  • 尝试+ 运算符来连接字符串,而不是按位的&amp;
  • @EliasVanOotegem 好点。
  • 感谢@MarkDee,它现在正在“工作”:)
【解决方案2】:

在 IE 中,变量 regionPhone 未定义。显然,正如 Mark 提到的,这是由于您的 return 声明(IE 说:return statement outside of function)。

如何将您的逻辑包装到 $(function() {}) 中,这将确保在加载 DOM 时运行逻辑。此外,它避免了使用全局变量。

类似:

$(function() {
    var userRegion = geoip_region();
    var userCountry = geoip_country_code();
    var regionPhone;

    switch (userCountry) { 
    case "GB":
    var region = {};  
        // and so on

    $(".Regional-Phone").html(regionPhone);
    document.write('<p>Country: ' + userCountry + '<br>Region: ' + userRegion + '<br>City: ' + geoip_city() + '</p>');

});

【讨论】:

    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多