我试图在点击时将动态创建的元素置于其他元素之上,如下所示:
$("#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?
}
}
})
答案 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');
});