2010-08-02 42 views
3

zindex的语法是什么?如何在JavaScript中使用zindex

我试过这样

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

它不工作,它甚至不显示错误。

这是我的代码片段。它在HTML中工作正常,但我想在jQuery或JavaScript中设置此CSS属性。顶部/位置属性不起作用...

z-index:1200px;顶:450像素;位置:绝对的;右:300像素;

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"; 

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

+0

为什么不把css行放在cssText中的元素? – 2010-08-02 14:32:28

+2

混合jQuery和纯JS(对于同一个任务)不太好。 – 2010-08-02 16:51:30

回答

8

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

在jQuery中,这将是

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

什么是职位... – Bharanikumar 2010-08-02 14:19:29

+2

@Bharani什么*关于*职位? – 2010-08-02 14:23:02

+0

plz chk我更新的问题... – Bharanikumar 2010-08-02 14:30:12

11

document.getElementById('iframe_div1').style.zIndex = "2000";是罚款。只要确保它是position就是absoluterelative。这样,

document.getElementById('iframe_div1').style.position = "relative"; // could also be absolute 
0
$('#iframe_div1').css({ 
     right : '300px', 
     top : '450px', 
     border : '1px solid red', 
     color : '#f00' 
}); 

由于问题固定

0

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

becouse:

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

必须是:

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

//or 

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

不,它不是。 “绝对”是用于“位置”,而不是“显示”。如果您不太了解该主题,请不要发布错误的答案。 – 2012-05-13 03:12:10