简体   繁体   English

jQuery min-width vs width:如何删除px?

[英]jQuery min-width vs width: How to remove px?

Trying some basic jQuery and JavaScript here. 在这里尝试一些基本的jQuery和JavaScript。

The .width() gives me an integer value. .width()给出了一个整数值。 The .css('min-width') gives me a value ending in px . .css('min-width')给出一个以px结尾的值。 Therefore I can't do math on this as is. 因此我不能按原样做数学。 What is the recommended work around? 推荐的工作是什么?

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')) {
  ...
}

Use replace() : 使用replace()

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

Alternatively, you could use the parseInt function: 或者,您可以使用parseInt函数:

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

The better way is to use parseInt . 更好的方法是使用parseInt jQuery functions .width() and .height() work quite so. jQuery函数.width().height()工作得非常好。

Also, it would be good to encapsulate fetching of this values in standalone functions: 此外,最好将这些值的获取封装在独立函数中:

  • .minHeight() , .minHeight( size ) , .minHeight( function() ) .minHeight() .minHeight( size ) .minHeight( function() )
  • .maxHeight() , ... .maxHeight()...
  • .minWidth() , ... .minWidth()...
  • .maxWidth() , ... .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()) {

一些字符串操作删除'px'然后使用parseInt将其作为数字:

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

If you pass the return value from .css('min-width') to parseInt first, it will remove the "px" for you. 如果首先将.css('min-width')的返回值传递给parseInt ,它将为您删除“px”。

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

(Don't forget the second parameter to parseInt .) (不要忘记parseInt的第二个参数。)

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

将子字符串用于从最小宽度返回的值以删除px

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM