2

我正在尝试使动态创建的元素在单击时位于其他元素之上,如下所示:

$("#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?
                }
            }
        })
4

2 回答 2

2

默认值为z-indexauto但是您可以大量简化代码以删除循环:

$("#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
});
于 2013-09-30T10:13:53.673 回答
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');
});
于 2013-09-30T10:16:36.380 回答