$(li).width()返回包含填充的值?

Sea*_*son 3 javascript css jquery width

http://jsfiddle.net/MeoMix/8zg2V/83/

我有以下jsFiddle.这个想法是当显示一个太长的上下文菜单项时,它会用省略号呈现,但是在鼠标悬停时它会翻阅文本.

有问题的代码:

//Provides helper methods for non-specific functionality.
define('helpers', [], function () {
    'use strict';

    return {
        scrollElementInsideParent: function(element, parent) {
            //  Scroll the element if its too long to read.
            $(element).mouseover(function () {

                var distanceToMove = $(this).width() - $(parent).width();

                console.log("My width and parent width:", $(this).width(), $(parent).width());

                $(this).animate({
                    marginLeft: "-" + distanceToMove + "px"
                }, {
                    //  Just a feel good value; scales as the text gets longer
                    duration: 15 * distanceToMove,
                    easing: 'linear'
                });

            }).mouseout(function () {
                $(this).stop(true).animate({ marginLeft: 0 });
            });
        }
    };
Run Code Online (Sandbox Code Playgroud)

在这里,我记录在宽度上滚动的元素及其父元素宽度.控制台输出:

我的宽度和父宽度:360 230

但在查看指标时,这似乎不正确:

在此输入图像描述

为什么是这样?

Ian*_*Ian 6

在您的代码中,parent参数引用<ul>.使用$(this).parent()得到<li>

演示: http ://jsfiddle.net/EsL2X/