Cách tốt hơn là sử dụng parseInt
. Các hàm jQuery .width()
và .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()) {
Nguồn
2011-08-03 07:57:27
Nó vẫn sẽ là một chuỗi, mặc dù. – pimvdb
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ó –
bạn muốn làm gì với kết quả?! –