读书人

下拉菜单有关问题?伸下来的菜单连接不

发布时间: 2012-03-30 17:32:10 作者: rapoo

下拉菜单问题?伸下来的菜单连接不了,现在就却这个项目就搞定了。谢谢大虾们。。
<!--使用层做下拉菜单或者漂浮图片的话,最大的问题就是会被输入框等挡住,更别说框架,所以现在比较流行用window.createPopup();的说,它可以在页面所有元素之上,不存在被谁遮挡的问题。

下面是刚写的一个简单的下拉菜单。代码很简单,很容易看懂,所以修改也方便。-->

<script language= "JavaScript " type= "text/JavaScript ">
//首先定义两个数组,一个是菜单文字,一个是链接
var MenuText = new Array(3);
var MenuEven = new Array(3);
MenuText[1] = [ "菜单11 ", "菜单12 ", "菜单13 "];
MenuText[2] = [ "菜单21 ", "菜单21 "];
MenuText[3] = [ "菜单31 ", "菜单321 ", "菜单33 "];

MenuEven[1] = [ "http://www.hao123.com ", "url12 ", "url13 "];
MenuEven[2] = [ "url21 ", "url22 "];
MenuEven[3] = [ "url31 ", "url32 ", "url33 "];

//初始化一个oPopup对象

var oPopup = window.createPopup();

//这两个是获得表格绝对位置的方法,很有用的说
function getLeftIE(x,m) {
var dx=0;
if (x.tagName== "TD "){
dx=x.offsetLeft;
}
else if (x.tagName== "TABLE ") {
dx=x.offsetLeft;
if (m) { dx+=(x.cellPadding!= " "?parseInt(x.cellPadding):2); m=false; }
}
return dx+(x.parentElement.tagName== "BODY "?0:getLeftIE(x.parentElement,m));
}
function getTopIE(x,m) {
var dy=0;
if (x.tagName== "TR "){
dy=x.offsetTop;
}
else if (x.tagName== "TABLE ") {
dy=x.offsetTop;
if (m) { dy+=(x.cellPadding!= " "?parseInt(x.cellPadding):2); m=false; }
}
return dy+(x.parentElement.tagName== "BODY "?0:getTopIE(x.parentElement,m));
}

//显示菜单
function ButtonClick(node,t)
{
//首先得到鼠标所在的表格的位置
var left = getLeftIE(node,true)+8;
var top = getTopIE(node,true)+18;
//定义打开的oPopup的宽度,高度
var width = 90;
var height = MenuText[t].length*20;
//清空oPopup的内容,否则每次调用都只会在后面接着写
oPopup.document.body.innerHTML = " ";
var oPopBody = oPopup.document.body;
oPopup.show(left,top, width, height, document.body);
oPopup.document.open;
//让oPopup在鼠标离开时自动隐藏。
oPopup.document.write( " <body leftmargin=0 topmargin=0 scroll=no style= 'border : 0 px; ' onmouseover=clearTimeout(parent.popt); onmouseout=parent.popt=setTimeout( 'parent.oPopup.hide() ',10);> ");

//用document.write()写菜单内容
oPopup.document.write( " <table width=100% height=100% style= 'border-collapse : collapse; '> ");
for(var i=0;i <MenuText[t].length;i++)
{
oPopup.document.write( " <tr> <td background= 'image/topbg.gif ' bgcolor=#cccccc style= 'border-top : #cccccc 1px solid;border-bottom : #666666 1px solid;border-left : #cccccc 1px solid;border-right : #666666 1px solid;mouse : hand; font-size : 12px; color:#000000;text-align : left;vertical-align : center;CURSOR: hand ' onmouseover=this.bgColor= '#C2D2E5 '; onmouseout=this.bgColor= '#cccccc '; height= '20 ' onclick= 'parent.parent.main.location.href=\ " "+MenuEven[t][i]+ "\ " ';> "+MenuText[t][i]+ " </td> </tr> ");


}
oPopup.document.write( " </table> </td> </tr> </table> ");
oPopup.document.write( " </body> ")
}


</SCRIPT>

<!--主要方法就这么多,由于我使用了框架,所以菜单代码中链接打开位置是下面的框架“main "。用下面的语句调用菜单显示方法ButtonClick()-->
<table>
<tr>

<td align= "left " onMouseOver= "ButtonClick(this,1) "> 菜单1 </td>
</tr>
</table>

<!--在此点击鼠标,菜单就会在此格的上面出现。菜单内容是上面那个数组 MenuText[1] = [ "菜单11 ", "菜单12 ", "菜单13 "];在其他地方显示其他菜单时只要修改数组和调用时的数字就行了。 -->

[解决办法]
<html>

<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> JK:支持民族工业,尽量少买X货 </title>
<script language= "javascript ">
var isResizing=false;
function Resize_mousedown(event,obj){
if(!obj.setCapture) return ;
isResizing=true;
obj.mouseDownX=event.clientX;
obj.leftTdW=obj.previousSibling.offsetWidth;
obj.setCapture();
}

function Resize_mousemove(event,obj){
if(!isResizing) return ;
var newWidth=obj.leftTdW*1+event.clientX*1-obj.mouseDownX;
if(newWidth> 1) obj.previousSibling.style.width = newWidth;
else {
obj.previousSibling.style.width=1;
obj.childNodes[0].innerHTML= "> ";
}
if(newWidth> 149) obj.childNodes[0].innerHTML= "< ";
}
function Resize_mouseup(event,obj){
if(!isResizing) return;
obj.releaseCapture();
isResizing=false;
}
function Resize_setDefault(event,obj){
if(obj.innerHTML== "< ") {
obj.parentNode.previousSibling.style.width=1;
obj.innerHTML= "> ";
}
else{
obj.parentNode.previousSibling.style.width=150;
obj.innerHTML= "< ";
}
event.cancelBubble=true;
}
</script>

<STYLE type= "text/css " remark= "ForMenu ">
.menuUl {
width:150px;border:#888888 1px solid;position:absolute;left:0px;top:17px;display:none;
}
.menuLi {
width:150px;border:#888888 1px solid;position:relative;background-color:white;height:18px;display:inline;
}
.menuUl .menuUl {
left:149px;top:2px;
}
.menuUl .menuLi {
display:block;
}

.menuLi A{
color:#0044ff;text-decoration:none;font:14px verdana;
}
.menuLi A:hover {
background-color:#f0d0d0;
}

</STYLE>
<SCRIPT language=javascript srcccccc= "../Menus/Menu_MouseFun.js " remark= "ForMenu " >
function Menu_mouseover(event,obj)
{
var toLi=event.srcElement||event.target;
if(toLi==obj) return ;
if(toLi.className!= "menuLi ") toLi=toLi.parentNode;
var subUl=toLi.getElementsByTagName( "div ");
if(subUl.length> 0) {
subUl[0].style.display= "block ";
if(subUl[0].insertAdjacentHTML && subUl[0].getElementsByTagName( "iframe ").length==0)//add a bgIframe for IE
{
subUl[0].insertAdjacentHTML( "afterBegin ", " <iframe style= 'z-index:-1;position:absolute;border:0px;width:100%;height: "+subUl[0].offsetHeight+ "; '> </iframe> ");
}
}
}
function Menu_mouseout(event,obj)
{
var fromLi=event.srcElement||event.target;
var toLi=event.toElement||event.relatedTarget;


if(toLi==null) toLi=obj;
if(fromLi.className!= "menuLi ") fromLi=fromLi.parentNode;
if(toLi.className!= "menuLi ") toLi=toLi.parentNode;
var toLiParent=toLi.parentNode||toLi;
var maxLevel=10;
while( maxLevel-- > 0)
{
if(fromLi.className!= "menuLi " || fromLi==toLi || fromLi==toLiParent|| fromLi==toLiParent.parentNode ) return;
var subUl=fromLi.getElementsByTagName( "div ");
if(subUl.length> 0){
if(subUl[0]==toLiParent) break;
subUl[0].style.display= "none ";
}
fromLi=fromLi.parentNode.parentNode;
}
}
</SCRIPT>
</head>

<body style= 'margin:0 ' scroll= "no ">
<table style= "width:100%;height:100%; " border=0 cellspacing=0 cellpadding=0 >
<tr style= "height:40px; "> <td colspan=3 nowrap >
<!--menu begin -->
Menu <br>
<div onmouseover= "Menu_mouseover(event,this); " onmouseout= "Menu_mouseout(event,this); " >
<div class= "menuLi " >
<A href= "# "> Services </A>
<div class= "menuUl " >
<div class= "menuLi " >
<A href= "# "> Web Design </A>
<div class= "menuUl " >
<div class= "menuLi " > <A href= "# "> Web Design-> sub </A> </div>
<div class= "menuLi " > <A href= "# "> Web Design-> sub2 </A> </div>
</div>
</div>
<div class= "menuLi " > <A href= "# "> Internet Marketing </A> </div>
<div class= "menuLi " > <A href= "# "> Hosting </A> </div>
<div class= "menuLi " > <A href= "# "> Domain Names </A> </div>
<div class= "menuLi " > <A href= "# "> Broadband </A> </div>
</div>
</div>
<div class= "menuLi " >
<A href= "# "> Services </A>
<div class= "menuUl " >
<div class= "menuLi " >
<A href= "# "> Web Design </A>
<div class= "menuUl " >
<div class= "menuLi " > <A href= "# "> Web Design-> sub </A> </div>
<div class= "menuLi " > <A href= "# "> Web Design-> sub2 </A> </div>
</div>
</div>
<div class= "menuLi " > <A href= "# "> Internet Marketing </A> </div>
<div class= "menuLi " > <A href= "# "> Hosting </A> </div>
<div class= "menuLi " > <A href= "# "> Domain Names </A> </div>
<div class= "menuLi " > <A href= "# "> Broadband </A> </div>
</div>
</div>
</div>
<!--menu end -->
</td>
</tr>
<tr style= "height:2px;line-height:2px;background-color:#cccccc; "> <td colspan=3 >
</td>
</tr>
<tr>
<td style= "width:150px; " >
<iframe zindex=1 id= "a " src= "http://www.google.com " style= "width:100%;height:100%; "> </iframe>


</td> <td style= "width:8px;cursor:e-resize;background-color:#cccccc;size:8px;z-index:10; " align= "center " valign= "middle "
onmousedown= "Resize_mousedown(event,this); " onmouseup= "Resize_mouseup(event,this); " onmousemove= "Resize_mousemove(event,this); " >
<font style= "background-color:#eeeeee;cursor:pointer; " onmousedown= "Resize_setDefault(event,this); "> < </font>
</td> <td>
<iframe zindex=1 id= "b " name= "ContentFrame " src= "http://www.baidu.com " style= "width:100%;height:100%;z-index:10 "> </iframe>
</td>
</tr>
</table>
</body>
</html>

读书人网 >JavaScript

热点推荐