1

My code, visually anyway, works fine, but I keep getting "Uncaught TypeError: Cannot Read property 'left' of undefined" in my console. I have no clue what might be causing it.

Specifically, the error is showing up on the following: line:

var left = $(this).siblings('li.active').offset().left - ($(this).parents('.hoverBar').offset().left + 20);**

The whole function for context:

 $('.hoverBar li').hover(function(){

        var left = $(this).offset().left - ($(this).parents('.hoverBar').offset().left + 20);
        var width = $(this).width() + "px";
        var sictranslate = "translate("+left+"px, 0px)";

        $(this).parent('ul').next('div.floatr').css({
            "width": width,
            "-ms-transform" : sictranslate,
            "-webkit-transform": sictranslate,
            "-moz-transform": sictranslate,
            "transform": sictranslate
        });

    },

    function(){

        var left = $(this).siblings('li.active').offset().left - ($(this).parents('.hoverBar').offset().left + 20);**
        var width = $(this).siblings('li.active').width() + "px";

        var sictranslate = "translate("+left+"px, 0px)";

        $(this).parent('ul').next('div.floatr').css({
            "width": width,
            "-ms-transform" : sictranslate,
            "-webkit-transform": sictranslate,
            "-moz-transform": sictranslate,
            "transform": sictranslate

        });

    }).click(function(){

        $(this).siblings('li').removeClass('active');

        $(this).addClass('active');

        return false;

    });

And some relevant HTML:

<nav class="head_nav">
<div class="hoverBar">
<ul id="navbar">
            <li class="active"><a href="">About</a>
    <ul>
        <li><a href="#">Mission</a></li>
        <li><a href="#">Board Members</a></li>
        <li><a href="#">Staff</a></li>
        <li><a href="#">Members</a></li>
        <li><a href="#">Task Forces</a></li>
            </ul>
    </li>
<li><a href="">Events</a>
    <ul>
        <li><a href="#">Description</a></li>
        <li><a href="#">Registration with Outlook ICS</a></li>
        <li><a href="#">Online Payment</a></li>
        <li><a href="#">Email auto-reminders</a></li>
        <li><a href="#">Multiple registrants allowed</a></li>
            </ul>
    </li>
</ul>
<div class="floatr"></div>
</div>
</nav>
4

2 回答 2

1

这是选择器没有元素的时候,偏移返回undefined。只需将您的代码包装成

var $siblings = $(this).siblings('li.active');
if ($siblings.length) {
    var left = $siblings.offset().left 
             - ($(this).parents('.hoverBar').offset().left + 20);
    var width = $siblings.width() + "px";

    //...
}

这样,您的选择器将被缓存到变量$siblings中,并且不会运行两次或更多次。

于 2013-09-30T20:07:41.083 回答
1

我将您的代码复制到 jsFiddle: http: //jsfiddle.net/mswieboda/F4fnf/并在第 16 行您需要添加.parents('.hoverBar').find('li.active')如下:

var left = $(this).parents('.hoverBar').find('li.active').offset().left - 
    ($(this).parents('.hoverBar').offset().left + 20);

这是因为悬停的元素$(this):.hoverBar > ul > li > ul > li > a不是.hoverBar > ul > li.active您试图在 DOM 中导航到的兄弟。

这修复了您在尝试.offset()时收到的 JS 错误,undefined因为该 jQuery 元素不存在。

于 2013-09-30T20:11:57.943 回答