为什么不这项工作与选择的jQuery元素括号[]
$('div.note')[0].removeClass('hidden');
我从萤火虫此错误:
TypeError: note.removeClass is not a function
为什么不这项工作与选择的jQuery元素括号[]
$('div.note')[0].removeClass('hidden');
我从萤火虫此错误:
TypeError: note.removeClass is not a function
$('div.note')[0]
提供的JavaScript对象,而不是一个jQuery对象, removeClass是一个jQuery方法。在使用jQuery方法之前,您需要将其转换为jQuery对象。
试试这个,
$($('div.note')[0]).removeClass('hidden');
或
$('div.note').eq(0).removeClass('hidden');
如果你不严格的支架使用,使用.eq()
是更加精简。
$('div.note').eq(0).removeClass('hidden');
非常基本,认为每个jQuery的通话作为给你的对象,看起来像这样:
var myPosts = $(".posts");
// To help make sense of it, myPosts looks something like this.
myPosts === {
elements : [ /* all of your returned elements */ ]
helpfulMethod : function() { for (element in elements) { /* .... */ } },
otherHelpfulMethod : function() { for (element in elements) { /*...*/ } }
}
如果你说:
var element = myPosts.elements[0];
element.otherHelpfulMethod();
它会在你吐的错误。
增加.element的方式是通过在事实之后调用数组作为“this”。
你也可以这样做:
for (i in arr; ....) {
this[i] = arr[i];
}
,并得到同样的效果。
所以调用myPosts [0]只是让你的HTML元素。 然后,您需要将其包装到一个jQuery对象中以使用辅助方法,或者您需要使用jQuery辅助函数来访问该特定元素。
哈哈,真棒,有没有逻辑的原因,为什么这样的作品,我试过不? – Costa 2012-07-19 18:33:37
@Costa因为你打开jQuery对象并将其作为javascript元素引用。这意味着你不能将jQuery逻辑应用到元素。 – Ohgodwhy 2012-07-19 18:34:03