带有设置参数的 jQuery 外部宽度?

标签 jquery

因为我也面临着problem with jQuery's autocomplete宽度不正确 -

我看到了这段代码:

_resizeMenu: function() {
    var ul = this.menu.element;
    ul.outerWidth( Math.max(    //<------
        ul.width( "" ).outerWidth(),
        this.element.outerWidth()
    ) );
}

但是据我所知,outerWidth 不采用 bool 参数以外的参数来指示边距。

我不认为他们这样做只是为了获得真值、假值,因为他们不需要 MAth.max 来做到这一点。他们可以使用||

我错过了什么?

最佳答案

查看 jQuery 的源码:

// Create innerHeight, innerWidth, height, width, outerHeight and outerWidth methods
jQuery.each( { Height: "height", Width: "width" }, function( name, type ) {
    jQuery.each( { padding: "inner" + name, content: type, "": "outer" + name }, function( defaultExtra, funcName ) {
        // margin is only for outerHeight, outerWidth
        jQuery.fn[ funcName ] = function( margin, value ) {
            var chainable = arguments.length && ( defaultExtra || typeof margin !== "boolean" ),
                extra = defaultExtra || ( margin === true || value === true ? "margin" : "border" );

            return jQuery.access( this, function( elem, type, value ) {
                var doc;

                if ( jQuery.isWindow( elem ) ) {
                    // As of 5/8/2012 this will yield incorrect results for Mobile Safari, but there
                    // isn't a whole lot we can do. See pull request at this URL for discussion:
                    // https://github.com/jquery/jquery/pull/764
                    return elem.document.documentElement[ "client" + name ];
                }

                // Get document width or height
                if ( elem.nodeType === 9 ) {
                    doc = elem.documentElement;

                    // Either scroll[Width/Height] or offset[Width/Height] or client[Width/Height], whichever is greatest
                    // unfortunately, this causes bug #3838 in IE6/8 only, but there is currently no good, small way to fix it.
                    return Math.max(
                        elem.body[ "scroll" + name ], doc[ "scroll" + name ],
                        elem.body[ "offset" + name ], doc[ "offset" + name ],
                        doc[ "client" + name ]
                    );
                }

                return value === undefined ?
                    // Get width or height on the element, requesting but not forcing parseFloat
                    jQuery.css( elem, type, extra ) :

                    // Set width or height on the element
                    jQuery.style( elem, type, value, extra );
            }, type, chainable ? margin : undefined, chainable, null );
        };
    });
});

如您所见,innerHeight、innerWidth、height、width、outerHeight 和outerWidth 都是使用参数marginvalue 创建的。

由于第一个参数不是 bool 值(typeof margin !== "boolean"),chainable 将为 true,这意味着 margin 参数将用作调用 jQuery.acess 时的值(chainable ? margin : undefined)。

这意味着它将调用 jQuery.style( elem, type, value, extra ) 其中 type 是“width”,valueMath.maxextra 的结果将为“border”。

通过调试 jquery-1.10.2.js#9746 @ http://jsfiddle.net/95b7K/ 亲眼看看发生了什么

关于带有设置参数的 jQuery 外部宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20188574/

相关文章:

javascript - 正则表达式替换 div 内部 html 中的字符串

javascript - jQuery 显示隐藏元素 - 在 ie7 中跳转

javascript - YayraBox 数据表 JavaScript CDN 与 Laravel 冲突

jquery - 成功 getJSON 操作后 data.length 未定义

javascript - 在 jquery 中使用 fontawesome

javascript - 将 JPlayer 的播放列表与播放器分开以启用移动设备滚动

javascript - 如何为Jquery创建的元素添加Angular的ng-click?

jquery - 插入优先点击功能?

jquery - 滚动可见时 colgroup 上的 CSS 背景颜色

javascript - Bootstrap : ScrollSpy and smooth scroll