javascript jQuery 最小宽度与宽度:如何删除 px?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6917188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:19:55  来源:igfitidea点击:

jQuery min-width vs width: How to remove px?

javascriptjquerywidthcss

提问by Dan

Trying some basic jQuery and JavaScript here.

在这里尝试一些基本的 jQuery 和 JavaScript。

The .width()gives me an integer value. The .css('min-width')gives me a value ending in px. Therefore I can't do math on this as is. What is the recommended work around?

.width()给我一个整数值。该.css('min-width')给我结束一个值px。因此,我无法按原样进行数学计算。推荐的工作是什么?

alert($('#<%=lstProcessName.ClientID%>').parent('.column4').width());
alert($('#<%=lstProcessName.ClientID%>').parent('.column4').css('min-width'));
alert($('#<%=lstProcessName.ClientID%>').parent('.column4').width() >= $('#<%=lstProcessName.ClientID%>').parent('.column4').css('min-width'));

if ($('#<%=lstProcessName.ClientID%>').parent('.column4').width() >= $('#<%=lstProcessName.ClientID%>').parent('.column4').css('min-width')) {
  ...
}

回答by James Hill

Use replace():

使用replace()

alert($('#<%=lstProcessName.ClientID%>').parent('.column4').css('min-width').replace('px', ''));

Alternatively, you could use the parseIntfunction:

或者,您可以使用该parseInt功能:

alert(parseInt('1px')); //Result: 1

回答by Alex Yatkevich

The better way is to use parseInt. jQuery functions .width()and .height()work quite so.

更好的方法是使用parseInt. jQuery 的功能.width().height()工作非常如此。

Also, it would be good to encapsulate fetching of this values in standalone functions:

此外,最好在独立函数中封装此值的获取:

  • .minHeight(), .minHeight( size ), .minHeight( function() )
  • .maxHeight(), ...
  • .minWidth(), ...
  • .maxWidth(), ...
  • .minHeight(), .minHeight( size ),.minHeight( function() )
  • .maxHeight(), ...
  • .minWidth(), ...
  • .maxWidth(), ...

Like this:

像这样:

(function($, undefined) {

    var oldPlugins = {};

    $.each([ "min", "max" ], function(_, name) {

        $.each([ "Width", "Height" ], function(_, dimension) {

            var type = name + dimension,
                cssProperty = [name, dimension.toLowerCase()].join('-');

            oldPlugins[ type ] = $.fn[ type ];

            $.fn[ type ] = function(size) {
                var elem = this[0];
                if (!elem) {
                    return !size ? null : this;
                }

                if ($.isFunction(size)) {
                    return this.each(function(i) {
                        var $self = $(this);
                        $self[ type ](size.call(this, i, $self[ type ]()));
                    });
                }

                if (size === undefined) {
                    var orig = $.css(elem, cssProperty),
                        ret = parseFloat(orig);

                    return jQuery.isNaN(ret) ? orig : ret;
                } else {
                    return this.css(cssProperty, typeof size === "string" ? size : size + "px");
                }
            };

        });

    });

})(jQuery);

And your code will turn to:

你的代码将变成:

alert($('#<%=lstProcessName.ClientID%>').parent('.column4').width());
alert($('#<%=lstProcessName.ClientID%>').parent('.column4').minWidth());
alert($('#<%=lstProcessName.ClientID%>').parent('.column4').width() >= $('#<%=lstProcessName.ClientID%>').parent('.column4').minWidth());
if ($('#<%=lstProcessName.ClientID%>').parent('.column4').width() >= $('#<%=lstProcessName.ClientID%>').parent('.column4').minWidth()) {

回答by FishBasketGordo

If you pass the return value from .css('min-width')to parseIntfirst, it will remove the "px" for you.

如果您将返回值从.css('min-width')to传递给parseInt第一个,它将为您删除“px”。

parseInt(
    $('#<%=lstProcessName.ClientID%>').parent('.column4').css('min-width'), 
    10
);

(Don't forget the second parameter to parseInt.)

(不要忘记第二个参数parseInt。)

回答by Mrchief

Some string manipulation to remove 'px' and then use parseIntto get it as a number:

一些字符串操作以删除“px”,然后使用parseInt它作为数字获取:

var minWidth = parseInt($('#<%=lstProcessName.ClientID%>').parent('.column4').css('min-width').replace('px', ''), 10)

回答by citizen conn

alert($('#<%=lstProcessName.ClientID%>').parent('.column4').css('min-width').substring(0,indexOf('px'));

回答by Samir Adel

Use substring to the value returning from the min-width to remove the px

使用从 min-width 返回的值的子字符串删除 px