2012-07-19 87 views

回答

8

$('div.note')[0]提供的JavaScript對象,而不是一個jQuery對象, removeClass是一個jQuery方法。在使用jQuery方法之前,您需要將其轉換爲jQuery對象。

試試這個,

$($('div.note')[0]).removeClass('hidden'); 

$('div.note').eq(0).removeClass('hidden'); 
+0

哈哈,真棒,有沒有邏輯的原因,爲什麼這樣的作品,我試過不? – Costa 2012-07-19 18:33:37

+1

@Costa因爲你打開jQuery對象並將其作爲javascript元素引用。這意味着你不能將jQuery邏輯應用到元素。 – Ohgodwhy 2012-07-19 18:34:03

3

如果你不嚴格的支架使用,使用.eq()是更加精簡。

$('div.note').eq(0).removeClass('hidden'); 
1

非常基本,認爲每個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輔助函數來訪問該特定元素。