如何在 JavaScript 中使用 zindex

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3388467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:24:07  来源:igfitidea点击:

How to use zindex in JavaScript

javascriptjquery

提问by Bharanikumar

What is the syntax for zindex?

zindex 的语法是什么?

I tried like this

我试过这样

document.getElementById('iframe_div1').style.zIndex =2000;
document.getElementById('iframe_div1').style.z-index =2000;

It is not working, it doesn't even show an error.

它不起作用,它甚至不显示错误。

This is my snippet. It is working fine in HTML, but I want to set this CSS property in jQuery or JavaScript. The top/position attributes are not working...

这是我的片段。它在 HTML 中运行良好,但我想在 jQuery 或 JavaScript 中设置此 CSS 属性。顶部/位置属性不起作用...

z-index:1200px; top:450px; position:absolute; right:300px;

z-index:1200px; 顶部:450px;位置:绝对;右:300px;

document.getElementById('iframe_div1').style.display='block';
$("#iframe_div1").css("z-index", "500");
$("#iframe_div1").css("right", "300");
$("#iframe_div1").css("top", "450");
document.getElementById('iframe_div1').style.position = "absolute";

This snippet is not as perfect as I expect. I want to move my div center of the page, but it just moving my div to the bottom of the page.

这个片段并不像我预期的那么完美。我想移动页面的 div 中心,但它只是将我的 div 移动到页面底部。

回答by Reigel

document.getElementById('iframe_div1').style.zIndex = "2000";is fine. Just make sure it's positionis absoluteor relative. like this,

document.getElementById('iframe_div1').style.zIndex = "2000";很好。只要确保它positionabsoluterelative。像这样,

document.getElementById('iframe_div1').style.position = "relative"; // could also be absolute

回答by Pekka

The former should work all right. Use a DOM inspector like Firebug's "inspect element" to find out whether maybe the value gets assigned, but doesn't have the desired effect.

前者应该可以正常工作。使用像 Firebug 的“检查元素”这样的 DOM 检查器来找出值是否被分配,但没有达到预期的效果。

In jQuery, it would be

在 jQuery 中,它将是

 $("#iframe_div1").css("z-index", "2000");

回答by James Black

As this link mentions, zIndexonly works if the element was positioned, so are you using something like position: absolute;?

正如这个链接提到的,zIndex只有在元素被定位时才有效,所以你使用的是像position: absolute;吗?

http://www.w3schools.com/jsref/prop_style_zindex.asp

http://www.w3schools.com/jsref/prop_style_zindex.asp

回答by ajile

...but it just moving my div bottom of the page...

...但它只是将我的 div 移动到页面底部...

becouse:

因为:

document.getElementById('iframe_div1').style.display='block';

Must be:

必须是:

document.getElementById('iframe_div1').style.display='relation';

//or

document.getElementById('iframe_div1').style.display='absolute';

回答by Bharanikumar

$('#iframe_div1').css({
        right : '300px',
        top : '450px',
        border : '1px solid red',
        color : '#f00'
});

Thanks Problem fixed

谢谢 问题已解决