jQuery - z-index操作和自动值而不是数字

Pat*_*rik 4 jquery z-index

当你add new按下按钮时,jQuery将新dragrable DIVs的插入CONTAINER.当我点击其中一些时DIVs,我想改变z-index价值.但jQuery无法将z-index值作为数字.它只显示'auto'...是否有解决方案显示真正的z-index值和increase it by 1

jsFiddle示例 - http://jsfiddle.net/ynternet/84nVQ/10/

HTML

<div id="add" style="background:yellow; width:100px;"> add new </div>
<div id="container"> </div>
Run Code Online (Sandbox Code Playgroud)

jQuery的

function handler() {
    if ($(this).find("#menu").length) {
        return;
    }
    var currentIndex = $(this).css('z-index');
    alert(currentIndex);
    var newIndex = currentIndex + 1;
    alert(newIndex);
    $(this).css('z-index', newIndex);
}
$("#add").on({
    click: function(e) {
        var timestamp = Date.now();
        var posx = Math.floor(Math.random() * 400);
        var posy = Math.floor(Math.random() * 400);
        $('#container').append(function() {
            return $('<div class="add_to_this" id="' + timestamp + '" style="left:' + posx + 'px; top:' + posy + 'px; ">Click me, drag a change z-index</div>').click(handler).draggable({
                containment: "#container",
                scroll: false,
                cursor: 'lock'
        });
    });
    }
});
Run Code Online (Sandbox Code Playgroud)

CSS

#container {
    width:500px;
    height:500px;
    background: palegoldenrod;
    position: relative;
    top:20px;
    left: 100px;
    padding: 0px;
}
.add_to_this {
    padding:5px;
    background:yellowgreen;
    position: absolute;
    display:inline-block;
    width:200px;
    height:50px;
    -moz-user-select: none;
    -khtml-user-select: none;
    -webkit-user-select: none;
    user-select: none;
    -o-user-select: none;
}
Run Code Online (Sandbox Code Playgroud)

Den*_*ret 5

代码中的两个问题:

  • 你没有真正z-index开始,因为你没有设置它,所以你有'auto'
  • 你操纵z-indexas字符串,因为你不解析它

你需要做什么 :

  • 在CSS中添加一个z-index: z-index:100;
  • 解析z-index:var currentIndex = parseInt($(this).css('z-index'), 10);

演示(没有警报,因为他们很烦人)