0
我有一个javascript对象(简化)。为什么我不能将此元素引用设置为变量?
var MyObject = (function(){
var propertyOne = $('.links li:first-child');
var propertyTwo = $('.links .active');
function startup()
{
propertyOne.addClass("class"); //Works
propertyTwo.addClass("class2"); //Does not work
$('.links .active').addClass("class2"); // Works
}
return {
init: function()
{
startup();
}
}
});
我选择propertyOne和propertyTwo的方式有什么区别?我不明白为什么
propertyOne.addClass("class"); //Works
$('.links .active').addClass("class2"); // Works
然而...
propertyTwo.addClass("class2"); //Does not work
谁能帮我解释一下?
+1。jQuery的长度/大小是perfiect方法来di agnose这样的条件http://api.jquery.com/length/ –
啊,你是对的,我想我知道你要去哪里。在DOM被加载后(在脚本的另一部分中),'active'类被应用。有没有什么方法可以将该元素设置为我的对象的属性? –
也许,但我需要看到更多的代码。您可以在运行您放入帖子的代码之前设置“主动”类吗?这将是一个非常简单的解决方案。如果没有,我真的需要看到更多的代码(HTML和JS)提供进一步的帮助。 – maxedison