简体   繁体   English

变量在函数中定义,但需要重新定义

[英]Variable is defined in the function but needs re-defining

I am using a JQuery mobile plugin and having strange problems with it. 我使用的是JQuery移动插件,并且遇到了奇怪的问题。

If I use it like below, it works. 如果我像下面这样使用它,它的工作原理。

    $("#mainPage").on("pageshow", function(e) {

        var availableTags = ['some', 'about', 'tags'];

        $("#searchField").autocomplete({
            target: $('#suggestions'),
            source: availableTags
        });
    });

However if use it like below, it does not work. 但是如果像下面那样使用它,它就不起作用。 But cached is defined in the function and correctly shows the id. cached是在函数中定义并正确显示id。 If I redefine cached in the function it works again. 如果我在函数中重新定义cached它再次工作。 Can anyone explain why? 有谁能解释为什么?

    var cached = $("#searchField");

    $("#mainPage").on("pageshow", function(e) {

        var availableTags = ['some', 'about', 'tags'];

        //Alert says searchField
        alert(cached.attr('id'));

        //If I uncomment below line it works.
        //cached = $("#searchField");

        cached.autocomplete({
            target: $('#suggestions'),
            source: availableTags
        });
    });

这似乎有效: http//jsfiddle.net/9uxtT/它与你的情况有什么不同?

It's a timing issue in the first version you are searching for the tag "#searchField" after pageshow when all jquery modules have been loaded and DOM is complete 在第一个版本中,当您加载所有jquery模块且DOM完成后,您在pageshow之后搜索标记“#searchField”时出现了计时问题

In the second version you are searching for the tag whenever the javascript is loaded which might sometimes not even find the #searchfield if the javascript is in the header of the same file. 在第二个版本中,无论何时加载javascript,您都在搜索标记,如果javascript位于同一文件的标题中,有时甚至可能找不到#searchfield。

To complete the answer: Need to take in consideration that JQuery selectors return Objects and not DOM Elements check this code to see that you get two different objects: 要完成答案:需要考虑JQuery选择器返回对象而不是DOM元素检查此代码以查看您获得两个不同的对象:

 var cached = $("#searchField");
 cached.test = 1;
 $("#p2").on("pageshow", function(e) {
     var test2 = $("#searchField");
 test2.test =2; alert(cached.test +"!="+ test2.test);
 });​ 

$("#searchField") is not defined when you are calling it outside the function, here's the simple way to cache it the first time the handler is called 在函数外部调用它时,没有定义$("#searchField") ,这是在第一次调用处理程序时缓存它的简单方法

var cached;

$("#mainPage").on("pageshow", function(e) {
    // This line will only call jQuery once
    cached = cached ||  $("#searchField");

    var availableTags = ['some', 'about', 'tags'];

    alert(cached.attr('id'));

    cached.autocomplete({
        target: $('#suggestions'),
        source: availableTags
    });
});

Here's how to do it without a global variable, by using a self calling function to wrap that variable in a closure 以下是如何在没有全局变量的情况下使用自调用函数将该变量包装在闭包中

$("#mainPage").on("pageshow", (function(){
    var cached;
    return function(e) {
        // This line will only call jQuery once
        cached = cached ||  $("#searchField");

        var availableTags = ['some', 'about', 'tags'];

        alert(cached.attr('id'));

        cached.autocomplete({
            target: $('#suggestions'),
            source: availableTags
        });
    }
})());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM