javascript代码和zindex(javascript code and zindex)

编程入门 行业动态 更新时间:2024-10-17 19:34:54
javascript代码和zindex(javascript code and zindex)

我在使用javascript更改z-index时遇到问题任何人都可以帮助我解决我的错误,

我想要做的是,一旦点击一个按钮,它就会出现在另一个盒子的顶部,但它似乎不起作用。

function toggleupload(){ var but = document.getElementById('picbutton').innerHTML; if (but == "Change Picture"){ document.getElementById('picbutton').innerHTML = "Hide upload box"; document.getElementById('uploadbox').style.zIndex = 2; document.getElementById('profilebasic').style.zIndex = 1; } if (but == "Hide upload box"){ document.getElementById('picbutton').innerHTML = "Change Picture"; document.getElementById('uploadbox').style.zIndex = 1; document.getElementById('profilebasic').style.zIndex = 2; } } #profilebasic{ width:300px; height:300px; z-index:2; background-color:#0F0; } #uploadbox { position:absolute; top:0px; left:0px; width:300px; height:300px; z-index:1; background-color:#F00; } #uploadbo{ text-align:center; width:300px; height:300px; z-index:3; } <div id="uploadbo"> <div id="profilebasic"> </div> <div id="uploadbox"> </div> <button onclick="toggleupload();" id="picbutton">Change Picture</button> </div>

I am having problems with getting the z-index to change using javascript can any one help me on where I am going wrong,

what I am trying to do is have a box appear on top on another box once a button is clicked but it does not seem to work.

function toggleupload(){ var but = document.getElementById('picbutton').innerHTML; if (but == "Change Picture"){ document.getElementById('picbutton').innerHTML = "Hide upload box"; document.getElementById('uploadbox').style.zIndex = 2; document.getElementById('profilebasic').style.zIndex = 1; } if (but == "Hide upload box"){ document.getElementById('picbutton').innerHTML = "Change Picture"; document.getElementById('uploadbox').style.zIndex = 1; document.getElementById('profilebasic').style.zIndex = 2; } } #profilebasic{ width:300px; height:300px; z-index:2; background-color:#0F0; } #uploadbox { position:absolute; top:0px; left:0px; width:300px; height:300px; z-index:1; background-color:#F00; } #uploadbo{ text-align:center; width:300px; height:300px; z-index:3; } <div id="uploadbo"> <div id="profilebasic"> </div> <div id="uploadbox"> </div> <button onclick="toggleupload();" id="picbutton">Change Picture</button> </div>

最满意答案

z-index属性仅适用于定位元素 ,因此您需要在profilebasic上显式设置位置,如:

#profilebasic { width:300px; height:300px; z-index:2; background-color:#0F0; position:absolute; }

jsFiddle示例

(请注意,我还必须在你的按钮上设置一些CSS,否则它会在你定位的div下结束。)

The z-index property only works on positioned elements, so you need to explicitly set the position on profilebasic like:

#profilebasic { width:300px; height:300px; z-index:2; background-color:#0F0; position:absolute; }

jsFiddle example

(note that I also had to set some CSS on your button otherwise it would have ended up under your positioned divs.)

更多推荐

本文发布于:2023-07-25 05:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1256802.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   javascript   code   zindex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!