我试图在窗口调整大小时更改div
元素的宽度,但由于调整大小功能,元素的宽度最后总是为
function resizeUploadField() {
var uploadInputField = $('.input-append');
var uploadSelectButton = $('.btn-file');
var uploadRemoveButton = $('.fileupload-exists');
if (uploadRemoveButton.length) {
}
console.log(uploadInputField.width());
uploadInputField.width(uploadInputField.width() - uploadSelectButton.outerWidth() - 2);
console.log(uploadInputField.width());
}
$(document).ready(function() {
var windowWidth = $(window).width();
resizeUploadField();
$(window).resize(function(event) {
console.log($(event.target).width());
if ($(event.target).width() != windowWidth) {
resizeUploadField();
windowWidth = $(event.target).width();
}
});
resizeUploadField
函数似乎工作正常,因为在onDocumentReady中调用时字段已正确调整大小
也许有人可以给我一个暗示出错的地方
答案 0 :(得分:0)
尝试重新声明windowWidth = $(window).width();
功能中的resize()
。
<强> E.g。强>
$(document).ready(function() {
var windowWidth = $(window).width();
resizeUploadField();
$(window).resize(function(event) {
windowWidth = $(window).width();
console.log($(event.target).width());
if ($(event.target).width() != windowWidth) {
resizeUploadField();
windowWidth = $(event.target).width();
}
});
});
希望它有所帮助。