jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jQuery – z-index操作和自动值而不是数字大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
当您添加新按钮时,jQuery会将新的可翻转DIV插入到CONTAINER中.当我点击其中一些DIV时,我想更改z-index值.但是jQuery无法将z-index值作为数字.它只显示自动’…是否有解决方显示真正的z-index值并将其增加1?

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

HTML

<div id="add" style="BACkground:yellow; width:100px;"> add new </div>
<div id="container"> </div>

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'
        });
    });
    }
});

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;
}

解决方法

代码中的两个问题:

>你没有真正的z-index,因为你没有设置它,所以你有’自动
>您将z-index操作为字符串,因为您不解析它

你需要做什么 :

>在css中添加一个z-index:z-index:100;
>解析z-index:var currenTindex = parseInt($(this).css(‘z-index’),10);

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

大佬总结

以上是大佬教程为你收集整理的jQuery – z-index操作和自动值而不是数字全部内容,希望文章能够帮你解决jQuery – z-index操作和自动值而不是数字所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。