WordPress企业主题定制/开发/优化

怎样盖住html页面上的SELECT(下拉框)元素

首页 » ASP » 怎样盖住html页面上的SELECT(下拉框)元素

怎样盖住html页面上的SELECT(下拉框)元素
下拉框
,即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>

分类与标签:

ASP

相关项目

  • WordPress外贸企业主题

  • 最近更新

  • 热门标签