原作者:不详
当层遇到下拉框时总是挡不了select框?其实这是IE的BUG,其它的浏览器没有这个问题,对于这个问题论坛里不少提出,在这里提供我的几种方法,各有各的好处,有错,有好的意见者提出,谢谢.
1.最直接的方法:隐藏下拉框.
下面提供的是一个比较通用的一组函数:
test.htm
------------
<script>
var HideElementTemp = new Array()
//点击菜单时,调用此的函数,菜单对象
function cal_hideElementAll(obj){
cal_HideElement("IMG",obj)
cal_HideElement("SELECT",obj)
cal_HideElement("OBJECT",obj)
cal_HideElement("IFRAME",obj)
}
function cal_HideElement(strElementTagName,obj){
try{
var showDivElement = obj
var calendarDiv = obj
var intDivLeft = cal_GetOffsetLeft(showDivElement)
var intDivTop = cal_GetOffsetTop(showDivElement)//+showDivElement.offsetHeight
//HideElementTemp=new Array()
for(i=0i<window.document.all.tags(strElementTagName).lengthi++){
var objTemp = window.document.all.tags(strElementTagName)[i]
if(!objTemp||!objTemp.offsetParent)
continue
var intObjLeft=cal_GetOffsetLeft(objTemp)
var intObjTop=cal_GetOffsetTop(objTemp)
if(((intObjLeft+objTemp.clientWidth)>intDivLeft)&&
(intObjLeft<intDivLeft+calendarDiv.style.posWidth)&&
(intObjTop+objTemp.clientHeight>intDivTop)&&
(intObjTop<intDivTop+calendarDiv.style.posHeight)){
//var intTempIndex=HideElementTemp.length//已经有的长度
//save elementTagName is stutas
//HideElementTemp[intTempIndex]=new Array(objTemp,objTemp.style.visibility)
HideElementTemp[HideElementTemp.length]=objTemp
objTemp.style.visibility="hidden"
}
}
}catch(e){alert(e.message)
}
}
function cal_ShowElement(){
var i
for(i=0i<HideElementTemp.lengthi++){
var objTemp = HideElementTemp[i]
if(!objTemp||!objTemp.offsetParent)
continue
objTemp.style.visibility=’’
}
HideElementTemp=new Array()
}
function cal_GetOffsetLeft(src){
var set=0
if(src &&src.name!="divMain"){
if (src.offsetParent){
set+=src.offsetLeft+cal_GetOffsetLeft(src.offsetParent)
}
if(src.tagName.toUpperCase()!="BODY"){
var x=parseInt(src.scrollLeft,10)
if(!isNaN(x))
set-=x
}
}
return set
}
function cal_GetOffsetTop(src){
var set=0
if(src &&src.name!="divMain"){
if (src.offsetParent){
set+=src.offsetTop+cal_GetOffsetTop(src.offsetParent)
}
if(src.tagName.toUpperCase()!="BODY"){
var y=parseInt(src.scrollTop,10)
if(!isNaN(y))
set-=y
}
}
return set
}
</script>
<select></select>
<select></select>
<div style="position:absoluteleft:0top:0width:100height:100background-color:red" onclick="cal_hideElementAll(this)">
点击让select隐藏
</div>
<br><br><br><br><br><br>
<input type="button" value="点击让select显示" onclick="cal_ShowElement()">
以上这种方法,如果对于select框数目少,相对固定的话,直接用obj.style.visibility="hidden"这样进行隐藏是更直接的.
2.Object对象的优先度较高,可以挡住select框
<OBJECT id=aa style="display:nonez-index:1000position:absolutetop:0left:0width:152height: 200" type="text/x-scriptlet" data="about:<body><div style=’position:absoluteleft:0top:0width:152height:200font:14color:whitebackground:blackborder:1 solid black’>test</div>"></OBJECT>
<select><option>hellohellohellohello</select><button onclick=aa.style.display=aa.style.display=="none"?"":"none">test</button>
这种方法虽然也简单,但对复杂的层是来说还不是好的解决方法
3.用iframe作载体
以下是一简单的例子:
<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>简单菜单</title>
<!--
提供定位函数,用iframe作载体,不会被select挡住
-->
<style id=s>
#div1{
position:absolute
z-index:100
width:100
height:130
background-color:#d2e8ff
border:1 solid black
}
div{cursor:handfont-size:12px}
a{text-decoration:nonecolor:redfont-size:12px}
</style>
</head>
<body>
<script>
function window.onload(){
var shtml=div1.innerHTML
var ifm=document.createElement("<iframe frameborder=0 marginheight=0 marginwidth=0 hspace=0 vspace=0 scrolling=no></iframe>")
ifm.style.width=div1.offsetWidth
ifm.style.height=div1.offsetHeight
ifm.name=ifm.uniqueID
div1.innerHTML=""
div1.appendChild(ifm)
window.frames[ifm.name].document.write(s.outerHTML+"<body leftmargin=0 topmargin=0>"+shtml+"</body>")
}
function show(){
with(document.all.img1){
x=offsetLeft
y=offsetTop
objParent=offsetParent
while(objParent.tagName.toUpperCase()!= "BODY"){
x+=objParent.offsetLeft
y+=objParent.offsetTop
objParent = objParent.offsetParent
}
y+=offsetHeight-1
}
with(document.all.div1.style){
pixelLeft=x
pixelTop=y
visibility=’’
}
}
function hide(){
document.all.div1.style.visibility=’hidden’
}
</script>
<img id=img1 onmouseover="show()" onmouseout="hide()" src="ie.gif"><br><select></select>
<div id=div1 onmouseover="style.visibility=’’" onmouseout="style.visibility=’hidden’" style="visibility:hidden">
<div href="http://www.csdn.net" onmouseover="style.backgroundColor=’highlight’" onmouseout="style.backgroundColor=’’" onclick="window.open(href)"&gt中国程序员</div>
<div href="http://www.webjx.com" onmouseover="style.backgroundColor=’highlight’" onmouseout="style.backgroundColor=’’" onclick="window.open(href)">webjx</div>
</div>
</body>
</html>
<script src="http://libs.useso.com/js/jquery/1.10.0/jquery.min.js"></script><script>
var $optList = $('.page04_01_form_select_list')
var $arrowBtn = $('.page04_01_form_select_but,.page04_01_form_select_input>span') //下拉按钮和选项框
$arrowBtn.click(function(){
// 这里假设展开状态是listdown,收起状态是listup
var $this = $(this)
if($this.hasClass('listup')){
listDown()
}else{
listUp()
}
})
$optList.on('click', '>div', function(){
$optList.prev().text($(this).text())
listUp()
})
function listDown(){
$optList.show()
$arrowBtn.removeClass('listup').addClass('listdown')
}
function listUp(){
$optList.hide()
$arrowBtn.removeClass('listdown').addClass('listup')
}
</script>
用div+css的ul、li结合script脚本实现下拉列表菜单,全部代码如下,复制在DW中预览即可看到效果,细节样式可以自行修改:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312" />
<title>DIV+CSS实现下拉列表菜单</title>
<style type="text/css">
<!--
ul {
margin: 0px
padding: 0px
list-style-type: none
}
li{
float:left
width:160px
margin-left:1px
}
ul li a{
display:block
font-size:12px
border:#CCCCCC 1px solid
padding:3px
text-decoration:none
color:#777777
margin-top:1px
text-align:center
}
ul li a:hover{
background-color:#0099CC
color:#FFFFFF
}
li ul{
display:none
top:20px
margin-top:1px
}
li:hover ul,li.over ul{
display:block
}
-->
</style>
</head>
<script type="text/javascript"><!--//--><![cdata]//><!--
startlist = function() {
if (document.all&&document.getElementByIdx) {
navRoot = document.getElementByIdx("nav")
for (i=0i<navRoot.childNodes.lengthi++) {
node = navRoot.childNodes[i]
if (node.nodeName=="li") {
node.onmouseover=function() {
this.className+=" over"
}
node.onmouseout=function() {
this.className=this.className.replace(" over","")
}
}
}
}
}
window.onload=startlist
//--><!]]></script>
</script>
<body>
<ul >
<li><a href="">文章</a>
<ul>
<li><a href="">CSS 教程</a></li>
<li><a href="">DOM 教程</a></li>
<li><a href="">XML 教程</a></li>
<li><a href="">Flash 教程</a></li>
</ul>
</li>
<li><a href="">参考</a>
<ul>
<li><a href="">XHTML</a></li>
<li><a href="">XML</a></li>
<li><a href="">CSS</a></li>
</ul>
</li>
<li><a href="">Blog</a>
<ul>
<li><a href="">全部</a></li>
<li><a href="">网页技术</a></li>
<li><a href="">UI 技术</a></li>
<li><a href="">Flash 技术</a></li>
</ul>
</li>
<li><a href="">摇滚</a>
<ul>
<li><a href="">纯音乐</a></li>
<li><a href="">古典金曲</a></li>
<li><a href="">UI 技术</a></li>
<li><a href="">Flash 技术</a></li>
</ul>
</li>
</ul>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)