创建隐藏的菜单

本文介绍了一种使用JavaScript创建可隐藏菜单的方法。通过监听点击事件来显示或隐藏子菜单,适用于网页应用中需要节省空间展示菜单选项的场景。文章详细展示了如何初始化菜单、设置样式及绑定事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建隐藏的菜单

/**
*创建隐藏的菜单  yu jian   

*************************
*arraySub: 子菜单的VALUE  array数组;
*aSubs: 子菜单的链接  array数组;
*id: 父菜单的ID;
*id: select的id
*/
var SubHead=Class.create();
SubHead.prototype = {
 initialize:function(arraySub,aSubs,id,_id){
  this.arraySub=arraySub;  
  this.id=document.getElementById(id); 
  this.aSubs=aSubs;
  this.isDis=false;
  this._id=document.getElementById(_id);
  if(_id){
   this._id.onclick=this.doClick.bindAsEventListener(this); 
  }
  else{
   this.id.onclick=this.doClick.bindAsEventListener(this); 
  }
   
  this.makeDiv();
  this.createSub();
 },
 makeDiv: function(){//在此设置该div样式
  this.subDiv=document.createElement("div");
  this.subDiv.className="yuy"
  this.id.appendChild(this.subDiv);
 },
   doClick:function () {     
  if(this.isDis)this.isDis=false;
  else this.isDis=true;
  this.showSub();
 },
 showSub:function(){
  if(this.isDis){
   this.subDiv.style.display="";
  }
  else{
   this.subDiv.style.display="none"
  }
 },
 createSub:function(){
  this.subDiv.style.display="none";
  var divs=new Array(this.arraySub.length);
  for(var i=0;i<this.arraySub.length;i++){
   divs[i]=document.createElement("div");
   divs[i].innerHTML='<img src="../../../../lib/images/group/arrow.gif" width="3" height="5">'+'<a '+'href='+this.aSubs[i]+'>'+this.arraySub[i]+'</a>';  
   this.subDiv.appendChild(divs[i]);
  } 
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值