在函數循環中,我需要進一步遍歷DOM。
$('.panel_wrapper .panel').each(function(index) {
// how can I append to this. For instance another class = "foo"
$(this) .foo /* does not work */
這與我想要的功能
$(this) = $('.panel_wrapper .panel');
$(this) .foo
下面是一個更好的例子做:
$('.panel-wrapper .panel').each(function(index) {
$('.panel-wrapper .panel:eq(' + index + ') .collection_alt_thumbs img:eq(0)').css({"border-color" : "#9B9B9B", "opacity" : 1});
});
在函數還有額外的操作是將在第一次操作之後但在一個條件語句之後執行,而不是必需的。可以在以下進行:
$('.panel-wrapper .panel').each(function(index) {
$(this) ('.collection_alt_thumbs img:eq(0)').css({"border-color" : "#9B9B9B", "opacity" : 1});
// conditional statement
$(this) /* some other element */
});
追加什麼?你想添加一個名爲「foo」的CSS類到與你的第一個選擇器相匹配的所有對象嗎?試着在你的問題中更精確。 – 2010-11-30 07:45:56