开发者

jquery - change CSS of element based on its index

Does anyone know why this jquery might not work?

Ultimately I will replace the "1" index with a variable but I can't even get this working at the moment.

$('.myClicker').click(function() {

    $("#selectBo开发者_C百科xContainer img").css({"background-color":"#FFF"});
    $("#selectBoxContainer img:eg(1)").css({"background-color":"#000"});

});


You should use eq(), not eg(). Also, I usually use another css() syntax, not sure if your will work. Try this:

$("#selectBoxContainer img:eq(1)").css('background-color', '#000');

1 will be the second element, 0 is the first.


You need the eq selector, not eg


All the previous answers regarding the misnamed eq are correct. As some additional info for you, if you are really using a variable for the index number you might find it easier using the eq method instead of string concatenating the selector.

This is always easier IMO:

var index = 1;
$("#selectBoxContainer img").eq(index).css({"background-color":"#000"});

Than this:

var index = 1;
$("#selectBoxContainer img:eq(" + index + ")").css({"background-color":"#000"});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜