2011-06-22 114 views
1

我要做到以下幾點:我可以結合jQuery選擇器嗎?

$("#dat_chk" + (i)).css("background-color", "#f6FFee"); 
$("#dat_opt" + (i)).css("background-color", "#f6FFee"); 
$("#dat_txt" + (i)).css("background-color", "#f6FFee"); 

是這些方法,我可以縮短到只有一個使用jQuery選擇?

+0

這是http://stackoverflow.com/questions/1752718/how-的副本做我選擇多於一個元素的使用jQuery的屬性選擇器? – JMax

+0

使用逗號分開選擇器,您可以在單個jquery語句中使用它 – abhijit

回答

-1
$('.class').css("background-color", "#f6FFee"); 
3
$("#dat_chk" + i + ", #dat_opt" + i + ", #dat_txt" + i).css("background-color", "#f6ffee"); 
+0

檢查語法 – Teneff

1

是。用,它們分開:

$("#dat_chk" + (i) + ", #dat_opt" + (i) + ", #dat_txt" + (i)).css("background-color", "#f6FFee"); 
2
var selector = '#dat_chk' + i + ', #dat_opt' + i + ', #dat_txt' + i; 

$(selector).css("background-color", "#f6FFee"); 
2

試試這個

$("#dat_chk" + i + ", #dat_opt" + i + ", #dat_txt" + i).css("background-color", "#f6FFee"); 
0

的選擇應該是這樣的:

$("#dat_chk" + i + ", #dat_opt" + i + ", #dat_txt" + i) 
1

除了使用逗號分隔符,你也可以使用.add

 $("#dat_chk" + i) 
    .add("#dat_opt" + i) 
    .add("#dat_txt" + i) 
    .css(...);