JQuery重置所有其他子元素的z-index

时间:2013-09-30 10:11:29

标签: javascript jquery css z-index

我试图在点击时将动态创建的元素置于其他元素之上,如下所示:

$("#clicks").on('click', '[id^=pin]', function (e) {
            for ($i = 0; $i <= $("#clicks").children('.pin').length;$i++){
                if ('pin' + $i == this.id) {
                    $(this).css({ 'z-index': '9999' });
                } else {
                    //How to set reset the z-index of other elements?
                }
            }
        })

2 个答案:

答案 0 :(得分:2)

z-index的默认值为auto,但您可以大规模简化代码以删除循环:

$("#clicks").on('click', '[id^=pin]', function (e) {
    $("#clicks .pin").css('z-index', 'auto'); // reset zIndex on all .pin elements
    $(this).css('z-index', 9999);             // set the clicked pin to the top
});

答案 1 :(得分:1)

$("#clicks").on('click', '[id^=pin]', function (e) {
    $('[id^=pin]').css('z-index', '0'); //set z-index to all elements here
    $(this).css('z-index', '9999');
});