2013-03-19 70 views
0

我有以下代碼:有兩個選擇中的每一個()不能與此達到:第一,孩子這一點:第二個孩子

var tabla_szam = 1; 
var etk_szam = 1; 
$(".etrend_ossze, .tabla_sel").each(function() { 
    if (etk_szam == 5) { 
     tabla_szam++; 
     etk_szam = 1; 
    } 
$((this).find(":first-child")).attr("id", "tabla_" + tabla_szam + "_" + etk_szam); //i want to reach the actual .etrend_ossze and .tabla_sel 
$((this).find(":second-child")).attr("id", "tabla_sel" + tabla_szam + "_" + etk_szam); 
etk_szam++; 
}); 

控制檯給我這個錯誤:類型錯誤:此.find不是一個函數。我想通過$ this作爲第一個和第二個元素來達到實際的.etrend_ossze和實際的.tabla_sel。

回答

1

更換

$((this).find(":first-child")) 
$((this).find(":second-child")) 

通過

$(this).find(":first-child") 
$(this).find(":nth-child(1)") //:second-child doesn't exist 
+0

閱讀我才意識到這有.etrend_ossze的第一和第二個孩子的影響,我想是的第一和第二個元素這是.etrend_ossze和.tabla_sel對不起,我的誤解 – user2186932 2013-03-19 18:25:04

0

讓我們看看這個行:

$((this).find(":first-child")) 

現在,讓我們把它分解一下:

$(
    (this).find(":first-child") 
) 

所以瀏覽器第一次做(this)(它返回DOM元素對象),然後嘗試呼叫find。對於find的調用結果,它只會調用jQuery函數$。但是,它永遠不會調用$,因爲在本機DOM元素上沒有find方法,所以出現錯誤,如您所示。

解決的辦法是擺脫多餘的括號:

$(this).find(":first-child") 
+0

謝謝,我把它們中的許多人 – user2186932 2013-03-19 18:42:04

0

這不是一個正確的方法:

$((this).find(":first-child")).attr("id"..... 

它應該是:

無論是這樣的:

$(this).find(":first-child").attr("id".... 

或這樣:

$(":first-child",this).attr("id"..... 

嗯...但是,我從來沒有聽說過或:second-child

相關問題