简体   繁体   English

Javascript成员变量未定义

[英]Javascript member variable undefined

function Class() {
    var self=this;
    self.searchfield=$('#search');
    self.resultbox=$('#searchresults');

    self.onSearchFieldChanged=function() {
        console.log("F: "+self.searchfield);
        console.log("R: "+self.resultbox);
    }
    self.searchfield.live("keyup",self.onSearchFieldChanged);
}
var INSTANCE=null;
Class.init=function() {
   INSTANCE=new Class();
}
$(document).bind("globalsloaded", Class.init);

As soon as I type something in #search input, the output on console is as following: 只要我在#search输入中输入内容,控制台上的输出如下:

F: undefined
R: undefined

So why are those two variables undefined? 那么为什么这两个变量未定义? They ARE in scope and should contain the corresponding jQuery objects. 它们在范围内,应包含相应的jQuery对象。

After 5 days of work I found the Problem: JQueryMobile 经过5天的工作,我发现了问题:JQueryMobile

What happens is: 会发生什么:

  1. deviceready event by phonegap deviceready事件
  2. domready event domready活动
  3. My init is called 我的init被叫了
  4. JQM extracts the input#search element to reinsert it again, surrounded by some style divs JQM提取input#search #search元素以再次重新插入,由一些样式div包围
  5. The div I saved in my init is no longer valid, as refers to the "old" dom element 我在init中保存的div不再有效,因为引用了“旧”dom元素

Now I am doing my initialization in the pageinit event of JQM. 现在我在JQM的pageinit事件中进行初始化。 No problems anymore. 没问题了。

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

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