簡體   English   中英

在jQuery中添加變量和多個選擇器

[英]Adding a variable along with multiple selectors in jQuery

如何在下面的#red green等中添加變量i

$( "#red'+i', #green'+i', #blue'+i'" ).slider({ 
    orientation: "horizontal", 
    range: "min", 
    max: 255, 
    value: 127, 
    slide: refreshSwatch, 
    change: refreshSwatch 
});

到目前為止,我對此一無所知,請告知。

如果要選擇id字符串中具有紅色,綠色或藍色的所有元素,則可以使用

$("[id*=red], [id*=green], [id*=blue]")
$("#red" + i + ", #green" + i + ", #blue" + i).slider({ 
    orientation: "horizontal", 
    range: "min", 
    max: 255, 
    value: 127, 
    slide: refreshSwatch, 
    change: refreshSwatch 
});

您可以像這樣傳遞給slider之前分配/創建字符串...

var selector = '#red' + i + ', #green' + i + ', #blue' + i;
$(selector).slider({ 
    orientation: "horizontal", 
    range: "min", 
    max: 255, 
    value: 127, 
    slide: refreshSwatch, 
    change: refreshSwatch 
});

但是IMO,您應該尋找簡化選擇器的方法。 當代碼增長時,這可能會變得很麻煩。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM