隐藏元素+Event

by redky

HTML

<div class="box">1</div>
<div class="box">2</div>

CSS

.box {
    width: 100px;
    height: 100px;
    background-color: #faa;
    margin: 10px;
    line-height: 100px;
    text-align: center;
}

.box:first-child {
    position: absolute;
    z-index: 2;
    background: #f2f2f2;
    /* visibility: hidden; */
    /* opacity: 0; */
}

JavaScript

var boxes = document.querySelectorAll('.box');
for ( var i = 0, l = boxes.length; i < l; ++i) {
    boxes[i].onclick = function(e) {
        alert( this.innerHTML );
    }
}