2014-01-22 127 views
-1

我通常使用不同风格的代码,这是一种更基本的风格。所以我正在尝试一种新的做事方式,但我似乎无法得到它。jQuery函数返回undefined

我的console.log返回一个jQuery对象:console.log(accountMenu.child);

但是这返回undefined:console.log(accountMenu.child.css('top'));。和其他jQuery方法是一样的。我究竟做错了什么?

var accountMenu = { 
accountButton: jQuery('.my-account-btn'), 
child: jQuery('#my-account-droplist'), 
container: jQuery('.my-account-droplist-container'), 

closeAccount: function() { 
    accountButton.removeClass('expanded'); 
    child.animate({ 
     "top": child.outerHeight() * -1 
    }, 650); 
}, 

openAccount: function(){ 
    accountButton.addClass('expanded'); 
    child.animate({ 
     "top": 0 
    }, 650); 
    this.setTimeout(function(){closeAccount()}, 6000); 
}}; 

jQuery(document).ready(function(){ 

accountMenu.child.css({"top": accountMenu.child.outerHeight() * -1}); 

console.log(accountMenu.child); 

//animate account drop down 
accountMenu.accountButton.click(function() { 
    //check if open or closed 
    if (accountMenu.accountButton.hasClass('expanded')){ 
     accountMenu.accountMenu.closeAccount(); 
    } else{ 
     //first close mycart 
     if (jQuery('.top-cart > div').hasClass('expanded')){ 
      Enterprise.TopCart.hideCart(); 
      setTimeout(function(){ 
       accountMenu.accountMenu.openAccount(); 
      }, 700); 
     }else { 
      accountMenu.accountMenu.openAccount(); 
     }; 
    }; 
}); 
}); 
+0

是jQuery的正确加载?什么时候'.css(“top”)'给出未定义 - 在'document.ready'之前或之后触发? – CompuChip

+0

已加载jQuery。 –

+0

我从** console.log(accountMenu.child); **: [context:document,selector:“#my-account-droplist”,jquery:“1.10.2”,constructor:function, init:function ...] 并从** console.log(accountMenu.child.css('top')); **: [div#my-account-droplist,context:document,selector:“# my-account-droplist“,jquery:”1.10.2“,constructor:function,init:function ...] –

回答

1

你知道jQuery(document).ready(),你已经在使用它。但是,在页面加载完成之前,您可以从该块外部的页面获取数据。

编辑:如果你问如何使accountMenu全球,你与var关键字在适当的范围内声明它:

var accountMenu; 

jQuery(document).ready(function(){ 
    accountMenu = { 
     accountButton: jQuery('.my-account-btn'), 
     ... 
    }; 
}); 
+0

aah好吧,这是有道理的。但我需要从其他地方访问数据,这就是为什么我把它放在ready()函数之外的原因。什么是更好的方式来格式化? –

0

“我的console.log返回jQuery对象:console.log(accountMenu.child);

这是因为$("any selector")总是返回一个jQuery OB ject,但如果没有元素匹配,jQuery对象将为空,然后.css()将返回undefined

试图访问/操作DOM元素的任何JS代码都需要在浏览器解析相关元素后运行,这意味着您应该将var accountMenu = { ...声明移动到文档就绪处理程序中或将脚本元素移至(在</body>标记之前 - 在这种情况下,您根本不需要就绪处理程序)。

+0

好吧,但那么我怎么会从jQuery(document).ready()函数以外调用这两个函数呢? –

+0

我不明白。你为什么不打电话给他们,就像你已经打电话给他们一样? – nnnnnn

+0

从哪里?你不是总想从某个触发器访问你的数据吗(无论是'document.ready'还是一些按钮点击等)? – CompuChip

0

哪些错误在侧Document.ready.I越来越可变accountMenu认为你需要做这样的

jQuery(document).ready(function(){ 

var accountMenu = { 
accountButton: jQuery('.my-account-btn'), 
child: jQuery('#my-account-droplist'), 
container: jQuery('.my-account-droplist-container'), 

closeAccount: function() { 
    accountButton.removeClass('expanded'); 
    child.animate({ 
     "top": child.outerHeight() * -1 
    }, 650); 
}, 

openAccount: function(){ 
    accountButton.addClass('expanded'); 
    child.animate({ 
     "top": 0 
    }, 650); 
    this.setTimeout(function(){closeAccount()}, 6000); 
}}; 

accountMenu.child.css({"top": accountMenu.child.outerHeight() * -1}); 

console.log(accountMenu.child); 

//animate account drop down 
accountMenu.accountButton.click(function() { 
    //check if open or closed 
    if (accountMenu.accountButton.hasClass('expanded')){ 
     accountMenu.accountMenu.closeAccount(); 
    } else{ 
     //first close mycart 
     if (jQuery('.top-cart > div').hasClass('expanded')){ 
      Enterprise.TopCart.hideCart(); 
      setTimeout(function(){ 
       accountMenu.accountMenu.openAccount(); 
      }, 700); 
     }else { 
      accountMenu.accountMenu.openAccount(); 
     }; 
    }; 
}); 
});