点击show span

时间:2013-06-03 05:35:31

标签: html css

我试图隐藏另一个跨度下的跨度,当有人点击最上面的跨度时,它应该消失并显示其下面的内容。

我的HTML:

<span id="couponCode" style="display:inline-block;border:2px dashed;padding:5px;margin:5px;background:#EE4000;color:white;height:20px;text-align:center;">{{ i.couponCode }}</span><span class="cTs">Click to see code</span>

我的CSS:

.cTs
{
    left :0px;
    padding:10px;
    height:50px;
    color:white;
    width: 100px;
    position: relative;
    background: black;  
}

但是我无法定位第一个跨度...而是出现在第一个跨度的右侧....有人可以帮助我......

1 个答案:

答案 0 :(得分:2)

将元素包含在position: relative;容器中,而不是将position: absolute;topleft值用于内部元素

div {
    position: relative;
}

div span {
    position: absolute;
    top: 0;
}

div span:nth-of-type(1) {
    z-index: 1;    
}

div span:nth-of-type(2) {
    top: 10px;
    left: 25px;
}

Demo

使用onclick事件