2011-08-02 37 views
7

Thử một số jQuery và JavaScript cơ bản tại đây.Chiều rộng và chiều rộng tối thiểu của jQuery: Cách xóa px?

.width() mang lại cho tôi giá trị số nguyên. .css('min-width') mang lại cho tôi giá trị kết thúc bằng px. Vì vậy, tôi không thể làm toán học như thế này. Công việc được đề xuất là gì?

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

Trả lời

20

Sử dụng replace():

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

Ngoài ra, bạn có thể sử dụng parseInt chức năng:

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

Một số chuỗi thao tác để loại bỏ 'px' và sau đó sử dụng parseInt để có được nó như là một số:

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

Sử dụng chuỗi giá trị trở về từ các min-width để loại bỏ các px

+0

Nó vẫn sẽ là một chuỗi, mặc dù. – pimvdb

+0

nhưng chuỗi một chuỗi hợp lệ để thực hiện bất kỳ thao tác nào trên nó –

+0

bạn muốn làm gì với kết quả?! –

2

Nếu bạn vượt qua giá trị trả về từ .css('min-width') để parseInt đầu tiên, nó sẽ loại bỏ các "px" cho bạn.

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

(Đừng quên tham số thứ hai để parseInt.)

9

Cách tốt hơn là sử dụng parseInt. Các hàm jQuery .width().height() hoạt động khá tốt.

Ngoài ra, nó sẽ là tốt để đóng gói quyến rũ của các giá trị này trong các chức năng độc lập:

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

Như thế này:

(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); 

Và mã của bạn sẽ chuyển sang:

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()) { 
+0

'jQuery.isNaN' chỉ nên là' isNaN'. Tôi biết bài viết này là cũ, nhưng câu trả lời này là bởi đến nay là tốt nhất ở đây. –

Các vấn đề liên quan