我通常使用不同风格的代码,这是一种更基本的风格。所以我正在尝试一种新的做事方式,但我似乎无法得到它。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();
};
};
});
});
是jQuery的正确加载?什么时候'.css(“top”)'给出未定义 - 在'document.ready'之前或之后触发? – CompuChip
已加载jQuery。 –
我从** 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 ...] –