2012-07-29 112 views
1

我試圖訪問元素的ID nextElementSibling下面圖片中所呈現的陣列中:jQuery的 - 遍歷DOM - 陣列 - 提取ID

items[3].item.nextElementSibling.attr('id')) 

該查詢不起作用。而我可以訪問該項目元素的ID如下:

items[3].item.attr('id')) 

有人可以解釋爲什麼第二個工作,而第一個不工作!? 感謝您的幫助!

enter image description here

enter image description here

回答

1

.attr()是一個jQuery的功能,但你不是一個jQuery對象($())上測試它。只需直接與正常javascipt的獲得其ID:

items[3].item.nextElementSibling.id 

或者在$()包裝它來訪問jQuery函數:

$(items[3].item.nextElementSibling).attr('id') 
+0

謝謝!我猜已經太晚了。 – 2012-07-29 21:50:16