我正在從PrototypeJS遷移到jQuery,並且遇到了將功能應用於添加到簡單的橫幅腳本中的新DOM元素的問題。jQuery show()不是一個函數 - 用於添加DOM元素
基本上,在頁面加載時,使用append()將新的DIV元素放置到DOM中,據我所知,這是在jQuery中完成的,與Prototype的Element對象相反。
$.each(Banner.data, function(i, e) {
$('#banner_area').append("<div class='banner_slot' id='bannner-"+ i +"'>...[nested elements]</div>").hide();
});
$('.banner_slot').get(0).show();
檢查Firebug時,元素已成功添加到DOM,並立即隱藏。然後第一個(0)元素應該顯示......然而,螢火蟲給我這個錯誤:
TypeError: $(...).get(...).show is not a function
http://www.ten103.com/javascript/global_desktop.js
Line 15
我敢肯定,這是簡單的東西,因爲我一直在使用原型多年,但需要移動到jQuery,因爲,更多的人使用它,所以資源無限更好。
我在這裏失蹤的兩者之間有一些根本的區別嗎?
,而不是'$( 'banner_slot ')。得到(0).show()',請嘗試使用'$('。banner_slot')[0 ] .show()' –
使用正確的方法,只需將'get'替換爲'eq' – adeneo
...或'.first()'。 – JJJ