Javascript 如何从jquery函数返回变量
你能帮我吗。。。 如何从jquery函数返回变量Javascript 如何从jquery函数返回变量,javascript,jquery,Javascript,Jquery,你能帮我吗。。。 如何从jquery函数返回变量 var height = $(window).height(); $(window).resize(function(){ var height = $(window).height(); return height; }); setInterval(function() { $('div').append( 'Index: ' + height ); }, 500); 当您使用var时,您正在创建一个新变量,而不
var height = $(window).height();
$(window).resize(function(){
var height = $(window).height();
return height;
});
setInterval(function() {
$('div').append( 'Index: ' + height );
}, 500);
当您使用
var
时,您正在创建一个新变量,而不是覆盖原始变量。您将需要以下内容:
var height = $(window).height();
$(window).resize(function() {
height = $(window).height();
});
// ...
你不需要回来。试试这个:
var height = $(window).height(); // define "height" here
$(window).resize(function(){
height = $(window).height(); // update "height" variable,
// don't user "var" here.
// because using "var" will redefine
// "height" again and no longer contain the
// updated value on window resize
// out of this resize function scope
});
setInterval(function() {
$('div').append( 'Index: ' + height );
}, 500);
我想你的问题的答案取决于你想如何回答。慰问?警觉的?div或其他元素?请参见下面的列表
- 控制台日志(高度)
- 警戒(高度)
- $(“#elementID”).html(高度);//或者任何适用于您的案例的选择器和方法