简体   繁体   中英

Adding a variable along with multiple selectors in jQuery

How can I add variable i in #red green etc. in following?

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

I have no clue about this so far, please advise.

如果要选择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 
});

You can assign/create the string before passing to slider like this...

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

But IMO, you should look for simplifying your selector. This could become hassle when code grows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM