如何盖住html页面上的SELECT(下拉框)元素

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

下拉框,即html的SELECT元素,.net设计时的DropDownList,是html中的windowed element,尤其ie6之后,几乎是唯一的windowed element(还有popup等少量极少用的的)。

普通的元素,textbox, div, table……这些,属于windowless element,它们之间互相遮盖的情况由z-index决定,在它们之上,是SELECT这些windowed element。所以一般情况下div、table等不能遮盖select。

这个问题广泛存在于各种弹出式控件的使用之中,比如日历控件等。

如果要显示div,以前的做法是,动态的,在显示的时候,让div区域的select不可见,div消失的时候,再恢复这些select元素。这种做法比较奇怪,因为它严格上并不是“遮盖”了select,而是,让她整个消失了,如果calendar弹出元素只是应该遮盖select元素的一部分,但select却整个不见,用户也许会觉得奇怪;做起来也麻烦,要用js逐一判断各select的位置。

ie5.5之后,有一个新的小技巧,称之为“iframe shim”(iframe加塞:p),可以真正的“遮盖”select元素。

它利用了一种特殊的元素:iframe。在ie5.5之前,iframe也是windowed element,但从5.5开始,iframe就是普通的windowless element了,可是,虽然是windowless element,iframe却可以盖住select。这种做法的原理就是:放一个iframe与你要显示的东西(比如说一个div)同样大小、位置,并设置z-index使得iframe在此DIV之下;这样,iframe遮盖了select,同时,iframe又在要显示的div的下面,div就露出来了。

限制:仅适用于ie5.5及以后版本。

参考文章链接:

http://dotnetjunkies.com/WebLog/jking/archive/2003/07/21/488.aspx

示例程序代码:

//html.select.hack.iframe shim.htm

<html>

<head>

<script>

function DivSetVisible(state)

{

var DivRef = document.getElementById('PopupDiv');

var IfrRef = document.getElementById('DivShim');

if(state)

{

DivRef.style.display = "block";

IfrRef.style.width = DivRef.offsetWidth;

IfrRef.style.height = DivRef.offsetHeight;

IfrRef.style.top = DivRef.style.top;

IfrRef.style.left = DivRef.style.left;

IfrRef.style.zIndex = DivRef.style.zIndex - 1;

IfrRef.style.display = "block";

}

else

{

DivRef.style.display = "none";

IfrRef.style.display = "none";

}

}

</script>

</head>

<body background="http://www.orkut.com/img/i_blau2.gif">

<form>

<select>

<option>A Select Box is Born ....</option>

</select>

</form>

<div

id="PopupDiv"

style="position:absolute;font:italic normal bolder 12pt Arial; top:25px; left:50px; padding:4px; display:none; color:#ffff00; z-index:100">

.... and a DIV can cover it up<br>through the help of an IFRAME.

</div>

<iframe

id="DivShim"

src="javascript:false;"

scrolling="no"

frameborder="0"

style="position:absolute; top:0px; left:0px; display:none;filter=progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);">

</iframe>

<br>

<br>

<a href="#" onclick="DivSetVisible(true)">Click to show DIV.</a>

<br>

<br>

<a href="#" onclick="DivSetVisible(false)">Click to hide DIV.</a>

</body>

</html>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航