你能帮我吗...如何从jquery函数返回变量
var height = $(window).height();
$(window).resize(function(){
    var height = $(window).height();
    return height;
});
setInterval(function() {
    $('div').append( 'Index: ' +  height );
}, 500);
    你能帮我吗...如何从jquery函数返回变量
var height = $(window).height();
$(window).resize(function(){
    var height = $(window).height();
    return height;
});
setInterval(function() {
    $('div').append( 'Index: ' +  height );
}, 500);
    你不需要回来。尝试这个:
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);
    当您使用时,var您正在创建一个新变量,而不是覆盖原始变量。你会想要这个:
var height = $(window).height();
$(window).resize(function() {
    height = $(window).height();
});
// ...
    我想你的问题的答案取决于你想如何返回它。安慰?警报?div 还是其他元素?请参阅下面的列表。
我想您也可以将该 $(window).resize 块转换为一个函数,然后调用该函数。这也应该有效。