IE6下Select的遮盖问题

这个问题可是ie6下相当经典的问题了。简单的说就是ie6下select永远在position:absolute的div上面,这个在想用div来做个对话框时经常会遇到。比方下面的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<body>
		<div>
			<select>
				<option>This is a select.</option>
			</select>
		</div>
		<div>This is upper div.</div>
	</body>
	<style type="text/css" title="">
	div{
		position:absolute;
		width:180px;
		height:60px;
	}
	.up{
		top:0;
		left:0;
		background:skyblue;
	}
	.down{
		top:15px;
		left:15px;
		background:yellowgreen;
	}
	</style>
</html>

因为在IE6中iframe是可以覆盖select,而div又可以覆盖iframe(一物降一物?),所以解决方法如下:

用iframe覆盖select

在up的div里添加一个iframe,大小至少要能覆盖select。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<body>
		<div>
			<select>
				<option>This is a select.</option>
			</select>
		</div>

		<div>
			<iframe></iframe>
			<div>This is upper div.</div>
		</div>
	</body>
	<style type="text/css" title="">
	div{
		position:absolute;
		width:180px;
		height:60px;
	}
	.up{
		top:0;
		left:0;
	}
	.up-inner{
		background:skyblue;
	}
	.down{
		top:15px;
		left:15px;
		background:yellowgreen;
	}

	.up .up-inner,.up iframe{
		position:absolute;
		width:100%;
		height:100%;
		top:0;
		left:0;
	}
	</style>
</html>

值得注意的是,如果iframe的allowTransparency="true"时是不能遮盖住select。这种情况下,如果要透明可以用filter:Aplha(Opacity=?)。

iframe遮盖select是个不错的解决方案,但并不完美。被iframe遮盖的select某种意义上“消失”了。如果将iframe的透明度调低,就会发现,select不在那里。。比方将上例中的up的宽度调低,并加上透明度,超囧的一幕就会出现:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<body>
		<div>
			<select>
				<option>This is a select.</option>
			</select>
		</div>

		<div>
			<iframe></iframe>
			<div>This is upper div.</div>
		</div>
	</body>
	<style type="text/css" title="">
	div{
		position:absolute;
		width:120px;
		height:60px;
	}
	.up{
		top:0;
		left:0;
		filter:alpha(Opacity=80);
	}
	.up-inner{
		background:skyblue;
	}
	.down{
		top:15px;
		left:15px;
		background:yellowgreen;
	}

	.up .up-inner,.up iframe{
		position:absolute;
		width:100%;
		height:100%;
		top:0;
		left:0;
	}
	</style>
</html>


已发布

分类

作者:

标签