如何在每次使用jQuery调整窗口大小时更新变量?
每次调整窗口大小时,如何更新另一个函数中的变量 代码如下:如何在每次使用jQuery调整窗口大小时更新变量?,jquery,Jquery,每次调整窗口大小时,如何更新另一个函数中的变量 代码如下: $(window).resize(function() { Here is the NewVarialble; }); (function (a) { OldVariable; Want to update the OldVarible with NewVariable; })(jQuery); 现在还不是100%清楚你有什么问题,但似乎只是关于范围的问题 这可能是有用的阅读
$(window).resize(function() {
Here is the NewVarialble;
});
(function (a) {
OldVariable;
Want to update the OldVarible with NewVariable;
})(jQuery);
现在还不是100%清楚你有什么问题,但似乎只是关于范围的问题 这可能是有用的阅读范围等 这是一个工作版本-在js上使用下面粘贴的代码,我不建议在window对象上使用变量,这只是为了演示 关于下面的评论 编辑:
$(document).ready(function () {// wrapper function / scope change as appropriate
var d = {
divalayerWidth: 0,
divalayerHeight: 0
};
$(window).resize(onWindowResize);
function onWindowResize(e){
updateD(d, e.target.outerWidth, e.target.outerHeight);
}
function updateD(d, w, h){
d.divalayerWidth = w;
d.divalayerHeight = h;
}
});
将变量设为全局变量(坏主意)或将resize块移动到自动执行匿名函数中,如下所示:
(function (a) {
OldVariable;
$(window).resize(function() {
this now has access to oldvariable
});
})(jQuery);
嗨,刘易斯,实际上我想在每次调整窗口大小时用新的windowWidth和windowHeight更新d.divlayerWidth和d.divlayerHeight值。当前脚本如下所示:(函数(a){var windowHeight=$(window.height();var windowWidth=$(window.width();a.divlayer=function(b,c){var d=this;d.init=function(){d.divlayerWidth=windowWidth;d.divlayerHeight=windowHeight;}}}}(jQuery);
$(document).ready(function () {// wrapper function / scope change as appropriate
var d = {
divalayerWidth: 0,
divalayerHeight: 0
};
$(window).resize(onWindowResize);
function onWindowResize(e){
updateD(d, e.target.outerWidth, e.target.outerHeight);
}
function updateD(d, w, h){
d.divalayerWidth = w;
d.divalayerHeight = h;
}
});
(function (a) {
OldVariable;
$(window).resize(function() {
this now has access to oldvariable
});
})(jQuery);