假设.我页面上有 100 个 按钮 . 每个按钮的onclick事件处理函数都会在页面弹出一个层 .并且这个层上有close按钮..我希望 当这100个按钮中有一个触发了 onclick事件,即页面上已经弹出一个层的情况下.其他99个按钮的onclick指向另一个处理函数.... 优雅..一定要优雅....最好不要创建全局变量 ...最好循环绑定事件只在 onload事件里只执行一次,而按钮的onclick事件里不要执行循环.... 最好.......最好今天不要加班.....

解决方案 »

  1.   

    按钮的 name 属性 都相同。抓取 那些 按钮,,事件触发的时候!,设置onclick 事件不过,楼主既然所有 按钮 同一触发事件,为什么定义时候,都一样不就OK?
      

  2.   


    // 写个10个按钮的 楼主可以试验下 看符合要求不<!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>
      <title> LightBox </title>
      <style type="text/css">
      #box {
        text-align:right;
        background-color: #fff;
        border: 20px solid #000000;
      }
      </style>
     </head>
     <body>
     <script type="text/javascript">
     <!--
    /****************************************
     *
     *    LightBox Bind the difference event
     *
     *
     ****************************************/
        var $ = function(id){
            return 'string' == typeof id ? document.getElementById(id) : id;
        }    var $d = (document.compatMode == 'CSS1Compat') ? document.documentElement : document.body;    var isIE = navigator.userAgent.indexOf('MSIE') != -1;
        var isIE6 = navigator.userAgent.indexOf('MSIE 6.0') != -1;
        isIE6 && document.execCommand('BackgroundImageCache', false, true);    var Extend = function(destination, source){
            for(var pro in source){
                destination[pro] = source[pro];
            }
            return destination;
        }    function addEvent(oTarget, sType, fnHandler){
            if(window.attachEvent){
                oTarget.attachEvent("on"+sType, fnHandler);
            }else if(window.addEventListener){
                oTarget.addEventListener(sType, fnHandler, false);
            }else{
                oTarget["on"+sType] = fnHandler;
            }
        }    function removeEvent(oTarget, sType, fnHandler){
            if(window.detachEvent){
                oTarget.detachEvent("on"+sType, fnHandler);
            }else if(window.removeEventListener){
                oTarget.removeEventListener(sType, fnHandler, false);
            }else{
                oTarget["on"+sType] = null;
            }
        }    function setOpacity(obj, o){
            if(!obj.currentStyle || !obj.currentStyle.hasLayout) obj.style.zoom = 1;
            if(!!isIE)obj.style.filter = 'alpha(opacity=' + Math.round(o) + ')';
            else obj.style.opacity = o / 100;
        }    var Bind = function(object, fun){
            return function(){
                fun.apply(object, arguments);
            }
        }    var Class = {
            create: function(){
                return function(){
                    this.initialize.apply(this, arguments);
                }
            }
        }    var OverLay = Class.create();
        OverLay.prototype = {
            initialize: function(options){
                this.SetOptions(options);
                Extend(this, this.options);
                this.Lay = document.body.insertBefore(document.createElement('div'), document.body.childNodes[0]);
                with(this.Lay.style){
                    position = 'fixed';left = top = '0px';width = height = '100%';zIndex = parseInt(this.zIndex);
                    backgroundColor = this.bgColor;display = 'none';
                }
                if(isIE6){
                    this.Lay.style.position = 'absolute';
                    this._resize = Bind(this, function(){
                        this.Lay.style.width = Math.max($d.clientWidth, $d.scrollWidth) + "px";
                        this.Lay.style.height = Math.max($d.clientHeight, $d.scrollHeight) + "px";
                    });
                    this.Lay.innerHTML += "<iframe style='position:absolute;left:0px;top:0px;widht:100%;height:100%;filter:alpha(opacity=0);z-index:-1'></iframe>";
                }
            },        Show: function(){
                if(isIE6){this._resize();addEvent(window,'resize',Bind(this, this._resize))};
                setOpacity(this.Lay, this.opacity);
                this.Lay.style.display = "block";                    
            },        SetOptions: function(options){
                this.options = {
                    bgColor: "#000000",
                    opacity: 80,
                    zIndex: 1000
                };
                Extend(this.options, options || {});
            },        Close: function(){            
                this.Lay.style.display = "none";
                isIE6 && removeEvent(window,'resize',Bind(this,this._resize));
            }
        }    var LightBox = Class.create();
        LightBox.prototype = {
            initialize: function(boxId, options){
                this.Box = $(boxId);
                this.Lay = new OverLay();
                this.SetOptions(options);
                Extend(this, this.options);            this.Box.style.display = "none";
                this.Box.style.zIndex = parseInt(this.Lay.zIndex) + 1;
                if(isIE6){
                    this._top = this._left = 0; 
                    this._fixed = Bind(this, function(){ this.isCenter ? this.SetCenter() : this.FixScroll(); });
                }
            },        SetCenter: function(){
                this.Box.style.marginLeft = $d.scrollLeft - this.Box.offsetWidth / 2 + "px";
                this.Box.style.marginTop = $d.scrollTop  - this.Box.offsetHeight / 2 + "px";
            },        FixScroll: function(){
                this.Box.style.top = $d.scrollTop - this._top + this.Box.offsetTop + "px";
                this.Box.style.left = $d.scrollLeft - this._left + this.Box.offsetLeft + "px";
                this._top = $d.scrollTop; this._left = $d.scrollLeft;
            },        Show: function(){
                this.hasLayer && this.Lay.Show();
                this.Box.style.position = this.isScroll && !isIE6 ? 'fixed' : 'absolute';    
                this.Box.style.display = "block";
                if(this.isCenter){
                    this.Box.style.left = "50%"; this.Box.style.top = "50%";
                    if(this.isScroll){
                        this.Box.style.marginLeft = -this.Box.offsetWidth / 2 + "px";
                        this.Box.style.marginTop =  - this.Box.offsetHeight / 2 + "px";
                    }else {
                        this.SetCenter();
                    }
                }else{
                    this.Box.style.left = "20%"; this.Box.style.top = "10%";
                }
                if(isIE6){
                    this.isCenter ? this.SetCenter() : this.isScroll && this.FixScroll();
                    this.isScroll && addEvent(window,'scroll',this._fixed);        
                }        },        SetOptions: function(options){
                this.options = {
                    hasLayer: true,
                    isCenter: true,
                    isScroll: true
                };
                Extend(this.options, options || {});
            },        Close: function(){
                this.Box.style.display = "none";
                this.Lay.Close();
            }
        }
     //-->
     </script>
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
     <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
      <input type="button" name="openbtn"  value="打开层" />
      <input type="button" name="openbtn" value="打开层" />
      <input type="button" name="openbtn" value="打开层" />
      <input type="button" name="openbtn" value="打开层" />
      <input type="button" name="openbtn" value="打开层" />
      <input type="button" name="openbtn" value="打开层" />
      <input type="button" name="openbtn" value="打开层" />
      <input type="button" name="openbtn" value="打开层" />
      <input type="button" name="openbtn" value="打开层" />
      <input type="button" name="openbtn" value="打开层" />
      <div id="box" style="width:300px;height:300px;">
        </br>
        <span id="clobtn">
           <input type="button" value="关闭"/>
        </span>
      </div>
     <select style="width:100px;">
        <option value="" selected="selected">Test IE6</option>
     </select>
     <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
     <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
     <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
     <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
      <script type="text/javascript">
      <!--
      // 只做测试用
      function anotherFun(){
    alert('bind this function');
      }   // 私有空间
      (function(){
    var box = new LightBox('box');
    var isbound = false;
    // 循环绑定按钮
    var obtn = document.getElementsByName('openbtn');
    for(var i = 0; i < obtn.length; i++){
    obtn[i].onclick = function(){
    if(!this._index && !!isbound){
     anotherFun()
    }else{
    box.Show();
    this._index = true

    }
    } $('clobtn').onclick = function(){
            box.Close();
    isbound = true;
        }  })();  //-->
      </script>
     </body>
    </html>
      

  3.   

    <!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/html; charset=gb2312" />
    <title>无标题文档</title>
    </head><body><div id="list">
    <button>点击</button>
    <button>点击</button>
    <button>点击</button>
    <button>点击</button>
    </div>
    <button id="close">关闭</button>
    <div id="msg">...</div><script type="text/javascript">(function(){
    var on = null;
    var msg = document.getElementById("msg");
    document.getElementById("close").onclick = function(){ on = null; msg.innerHTML = "关闭一个层了"; }

    var btns = document.getElementById("list").getElementsByTagName("button");
    for(var i=0,n=btns.length;i<n;i++){
    btns[i].onclick = check;
    }

    function check(e){
    var elm = e ? e.target : window.event.srcElement;
    if(!on){
    on = elm;
    msg.innerHTML = "显示一个层了";
    }else if(on!=elm){
    alert("执行另一个函数");
    }
    }
    })();</script></body>
    </html>
      

  4.   

    to 4#请问有更优雅的方案吗?   有办法让 onclick 事件处理函数check  和 关闭按钮的处理函数 不要共享同一个 on 元素吗另外请教你一下. 为什么外部的函数结束以后. 对象on还存在 ? 是因为传说中的闭包吗 ?请指教.谢谢
      

  5.   

    用jquery试了下
    jquery下载地址:http://jquery.com/<!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>
    <script language='javascript' src='js/jquery-1.3.2.js'></script><!-- 注意js路径 -->
    <script language='javascript' type='text/javascript'>
    $(document).ready(
    function()
    {
    var Btn=$("input[type='submit']:not(#BtnClose)");
    var Method=function()
    {
    alert("执行弹出层函数");
    Btn
    .unbind("click")
    .bind("click",
    function()
    {
    alert("已经有弹出层");
    }
    );
    }
    Btn.bind("click",Method);
    $("#BtnClose").bind("click",
    function()
    {
    Btn
    .unbind("click")
    .bind("click",Method);
    alert("关闭了弹出层");
    }
    );


    );
    </script>
    </head>
    <body>
    <input type="submit" value="按钮1"/>
    <input type="submit" value="按钮2"/>
    <input type="submit" value="按钮3"/>
    <input type="submit" value="按钮4"/>
    <input type="submit" value="按钮5"/>
    <input type="submit" value="按钮6"/>
    <input type="submit" id="BtnClose" value="关闭"/>
    </body>
    </html>
      

  6.   

    高效的方法:
    <input type="hidden" value="">
    我知道,这样不优雅,可是很实用。
    写个闭包?没有必要,那会浪费时间。
    真的到了开发,周期是最重要的