带有z-index和jquery的display-inline-block

时间:2013-04-25 13:37:03

标签: javascript html css

大家早上好,

我似乎遇到了一个小问题。我想要一个div覆盖另一个div(即在顶部),但zIndex不起作用。我怀疑原因是显示:内联块,但我需要保留它,以便正确显示网页。如何将div叠加到另一个?

以下是解释问题的jsfiddle:

http://jsfiddle.net/Yf7zD/

或者代码就在这里:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
<script type="text/javascript" src="javascript/cookies.js"></script>
<style>
#gameTable {
    font-size: 0;
    width: 840px;
    height: 240px;
    margin-top: 20px;
    margin-left: 78px;
    position: relative;
}
.iamdroppable {
    display: inline-block;
    margin: 0;
    padding: 0;
    border: 3px solid #FFF;
}

</style>
</head>

<body style="background-color: black;">
<div id="container" style="position:relative; border: solid 3px red;">
<div id="gameTable"><p>GameTable</p>
</div>
</div>
</body>
<script>
$(document).ready(function(e) {
var myId;
    for(vertical = 0; vertical < 3; vertical++) {
    for(horizontal = 1; horizontal <= 12; horizontal++) { //Outer Numbers
            if(vertical == 0)
            myId = horizontal * 3;
            else if(vertical == 1)
            myId = horizontal * 3 - 1;
            else
            myId = horizontal * 3 - 2;
            $('<div>', {//Normal numbers
                class: 'iamdroppable',
                id: '' + myId,
                width: '62px',
                height: '78px',
            }).appendTo('#gameTable');
    }
    }
    $('<div>', {//Quads
                class: 'iamdroppable',
                id: '1000',
                top: '-100px',
                width: '100px',
                height: '200px',
                zIndex: '1000',
                position: 'absolute',
            }).appendTo('#container');
            });
</script>
</html>

谢谢!

2 个答案:

答案 0 :(得分:0)

var div = $('<div>', {//Quads
            class: 'iamdroppable',
            id: '1000',
            top: '-100px',
            width: '100px',
            height: '200px',
            zIndex: '1000',
            position: 'absolute',
        }).appendTo('#container');
        });
$('body').append(div);

答案 1 :(得分:0)

您正在添加CSS样式作为属性,它们应该都在样式属性中:

$(document).ready(function (e) {
         var myId;
         for (vertical = 0; vertical < 3; vertical++) {
             for (horizontal = 1; horizontal <= 12; horizontal++) { //Outer Numbers
                 if (vertical === 0) myId = horizontal * 3;
                 else if (vertical == 1) myId = horizontal * 3 - 1;
                 else myId = horizontal * 3 - 2;
                 $('<div>', { //Normal numbers
                     class: 'iamdroppable',
                     id: '' + myId,
                     style: 'width:62px;height:78px;'
                 }).appendTo('#gameTable');
             }
         }
         $('<div>', { //Quads
             class: 'iamdroppable',
             id: '1000',
             style: 'top:-100px;width:100px;height:200px;z-index:1000;position:absolute;'
         }).appendTo('#container');
     });

另一方面,您正在进行此比较:if (vertical == 0)您应该使用三个===,如下所示:if (vertical === 0)