【问题标题】:JavaScript errors in IE8 - expected ';' & 'position().left' is nullIE8 中的 JavaScript 错误 - 预期为 ';' & 'position().left' 为空
【发布时间】:2013-10-01 23:39:55
【问题描述】:

有人可以帮帮我吗,在完成之前我无法完成网站,这对我的家人来说是一个惊喜:(

我在 IE8 中遇到了一个错误。此功能在所有浏览器上都能正常运行。为什么 IE 会标记错误,我根本不明白,我只能看到在修复之前没有出路:(请帮帮我,一定有一个小故障导致了明显不可见的错误......

这些是错误,如果有人可以帮助我,我将不胜感激。我对脚本感到困惑。

** 错误**

消息:'position().left' 为空或不是对象行:44 字符:5 代码:0

这是第 44 行 - 位于脚本中:

$magicLineTwo

这是脚本的全部代码,这个脚本中没有其他内容,有两组标记为“示例1和示例2”,第二个是问题:

 $(function(){

    var $el, leftPos, newWidth,
        $mainNav = $("#example-one"),
        $mainNav2 = $("#example-two");

    /*
        EXAMPLE ONE
    */
    $mainNav.append("<li id='magic-line'></li>");

    var $magicLine = $("#magic-line");

    $magicLine
        .width($(".current_page_item").width())
        .css("left", $(".current_page_item a").position().left)
        .data("origLeft", $magicLine.position().left)
        .data("origWidth", $magicLine.width());

    $("#example-one li").find("a").hover(function() {
        $el = $(this);
        leftPos = $el.position().left;
        newWidth = $el.parent().width();

        $magicLine.stop().animate({
            left: leftPos,
            width: newWidth
        });
    }, function() {
        $magicLine.stop().animate({
            left: $magicLine.data("origLeft"),
            width: $magicLine.data("origWidth")
        });    
    });


    /*
        EXAMPLE TWO
    */
    $mainNav2.append("<li id='magic-line-two'></li>");

    var $magicLineTwo = $("#magic-line-two");

    $magicLineTwo
        .width($(".current_page_item_two").width())
        .height($mainNav2.height())
        .css("left", $(".current_page_item_two a").position().left)
        .data("origLeft", $(".current_page_item_two a").position().left)
        .data("origWidth", $magicLineTwo.width())
        .data("origColor", $(".current_page_item_two a").attr("rel"));

    $("#example-two li").find("a").hover(function() {
        $el = $(this);
        leftPos = $el.position().left;
        newWidth = $el.parent().width();
        $magicLineTwo.stop().animate({
            left: leftPos,
            width: newWidth,
            backgroundColor: $el.attr("rel"),
        });
    }, function() {
        $magicLineTwo.stop().animate({
            left: $magicLineTwo.data("origLeft"),
            width: $magicLineTwo.data("origWidth"),
            backgroundColor: $magicLineTwo.data("origColor")
        });    
    });
});

【问题讨论】:

  • 你为什么让你的家人被IE8折磨?
  • 你的小提琴没有id为example-2的元素。我遇到了一个完全不同的错误。
  • 您的标记是否包含类current_page_item_two 的元素?我将您的标记加倍并添加了#example-two 和一个带有current_page_item_two 的li,它在IE8\Chrome 中运行良好。
  • 您遇到了哪个错误?这是与小提琴中的脚本相同的脚本,这里没有更多内容。应该是菜单,我想保持相同的效果。现在已经有几个星期了:(
  • 在你的小提琴中我得到Uncaught TypeError: Cannot call method 'height' of null,因为$mainNav2在DOM上不存在

标签: javascript jquery internet-explorer runtime-error


【解决方案1】:

这有很多错误,所以我不会详细介绍(例如,一页上有多个 jQueries,引用的元素不在 DOM 上等)。

这在我的机器上的 IE8 中有效,您可以随意修复有问题的样式。在 Chrome 或 IE 中没有显示 JS 错误:

http://jsfiddle.net/KyleMuir/ghAkB/15/embedded/result/

这里是固定的JS:

$(function () {

    var $el, leftPos, newWidth,
    $mainNav = $("#example-one")
    /*
    EXAMPLE ONE
*/
    $mainNav.append("<li id='magic-line'></li>");

    var $magicLine = $("#magic-line");

    $magicLine.width($(".current_page_item").width())
        .css("left", $(".current_page_item a").position().left)
        .data("origLeft", $magicLine.position().left)
        .data("origWidth", $magicLine.width());

    $("#example-one li").find("a").hover(function () {
        $el = $(this);
        leftPos = $el.position().left;
        newWidth = $el.parent().width();

    $magicLine.stop().animate({
        left: leftPos,
        width: newWidth
    });
    }, function () {
        $magicLine.stop().animate({
            left: $magicLine.data("origLeft"),
            width: $magicLine.data("origWidth")
        });
    });
});

小提琴:http://jsfiddle.net/KyleMuir/ghAkB/15/

希望这会有所帮助。

【讨论】:

  • 所以你删除了第二个例子,那么这部分脚本没用吗?你能不能提一下,如果你能做到,那么导致这个错误的最糟糕的事情是什么?没有第二个例子,现在运行完美,你做了一个了不起的工作,相信我,我非常感谢你的时间和你的帮助,这是一个巨大的解脱!太感谢了!!你是唯一能够对代码进行排序的人!! :)
  • 我希望你不介意我问别的,因为只有天才而且这个编码是一个完整的迷宫......我需要替换这两个代码 js/jquery-1.3 .1.min.js ajax.googleapis.com/ajax/libs/jquery/1.4/…' 对于更新的版本,我需要更新两个还是一个对所有功能都是通用的?我应该下载哪个版本,这也会影响我拥有的 jquery 功能吗?
  • 是的。它指向一个你没有的菜单。如果这对您有帮助,请将其标记为正确答案:) 很高兴能提供帮助。
  • 你的页面上应该只有一个 jQuery 文件,除非你有充分的理由拥有多个文件(例如插件需要旧版本),因为你需要开始担心noConflict。我建议您使用最新版本,我相信它是 1.10。
  • 您可以在此处下载最新版本:jquery.com 或通过链接到类似于此&lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"&gt;&lt;/script&gt; 的地址从 CDN 使用它,白色闪烁很可能是因为 javascript 尚未完成加载.不过很难说。
猜你喜欢
  • 2013-05-01
  • 1970-01-01
  • 2011-08-08
  • 2010-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
相关资源
最近更新 更多