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 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);

关于javascript - 如何从jquery函数返回变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12431241/

相关文章:

javascript - 使用 jqGrid 的最低 jQuery 版本

javascript - 如何在 Mobile Safari 中设置内容可编辑的 div 的文本插入位置?

javascript - 动态调用 jQuery 函数

javascript - 内容更改后如何使 onload 脚本再次工作

jquery - 多模态对话框滚动条 Bootstrap v.3.3.5 运行不正常

Javascript比较从对象属性中获取的数字作为字符串

javascript - forEach 循环中的 setTimeout

javascript - jQuery 单选按钮检查添加类,否则如果单选按钮未选中则删除类?

javascript - 如何在后面的代码中获取在客户端创建的文本框的值?

javascript - inputLocalFont.addEventListener 不是函数