代码之家  ›  专栏  ›  技术社区  ›  Albert

单击Div内的复选框时调用Div onblur事件

  •  1
  • Albert  · 技术社区  · 16 年前

    我有一个弹出的div,当用户单击div外部时,我想隐藏它。在div内部,我有一个复选框。。。

    问题是,当尝试单击复选框时,会触发div的onblur事件。我将cancelEventBubble代码放在复选框的onclick上,但onblur在复选框onclick事件之前触发。。。

    有什么想法或建议吗?这看起来很简单,但不是很简单。。。

    艾伯特

        <!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>Untitled Page</title>
    <style type="text/css">
    .popup
    {
        display: none;
        position: relative;    
        border: solid 1px black;
        width: 100px;
        left: 100px;
        top: 100px;
    }
    
    .lookupButton
    {
        width: 15px;
        height: 20px;
        background-color: blue;
    }
    
    .lookup
    {
        border: solid 1px green;
    }
    </style>
    <script language="javascript" type="text/javascript">
    var popupVisible = false;
    
    function cancelEventBubble(e) {
         if (!e) var e = window.event;
         e.cancelBubble = true;
         if (e.stopPropagation) e.stopPropagation();
    }
    
    function showPopup(obj)
    {
        if (!popupVisible)
        {
            popupVisible = true;
            document.getElementById("popup").style.display = "block";
            document.getElementById("popup").focus();
        }
        else
        {
            popupVisible = false;
            document.getElementById("popup").style.display = "";
        }
    }
    
    function hidePopup()
    {
        popupVisible = false;
        document.getElementById("popup").style.display = "";
    }
    
    function setTextValue(obj)
    {
        var combo = document.getElementById("cbo1");
        if (combo.value.indexOf(obj.value) == -1)
        {
            combo.value += obj.value + "; ";
        }
        else
        {
            combo.value = combo.value.replace(obj.value + "; ", "");
        }
    }
    
    </script>
    </head>
    <body>
    <table><tr><td>
        <input readonly="readonly" type="text" name="cbo1" id="cbo1" />
    </td><td>
        <div class="lookupButton" onclick="showPopup(this);"></div>
    </td></tr></table>
    
    <div id="popup" class="popup" onblur="hidePopup()">
        <input id="chk0" type="checkbox" onclick="cancelEventBubble(event); setTextValue(this);" value="John" />John<br />
        <input id="chk1" type="checkbox" onclick="cancelEventBubble(event); setTextValue(this);" value="Jack" />Jack<br />
        <input id="chk2" type="checkbox" onclick="cancelEventBubble(event); setTextValue(this);" value="James" />James<br />
        <input id="chk3" type="checkbox" onclick="cancelEventBubble(event); setTextValue(this);" value="Richard" />Richard<br />
        <input id="chk4" type="checkbox" onclick="cancelEventBubble(event); setTextValue(this);" value="Tim" />Tim<br />
    </div>
    </body>
    </html>
    
    2 回复  |  直到 16 年前
        1
  •  3
  •   Christian C. Salvadó    16 年前

    论现代浏览器 onblur 事件不会与一起激发 div event delegation ,将单击事件处理程序绑定到文档,并在单击的元素不是复选框或 lookupButton ,例如:

    document.onclick = function (e) { 
      e = e || window.event; 
      var element = e.target || e.srcElement; 
    
      if (element.tagName != "INPUT" && element.type != "checkbox" &&
          element.className != "lookupButton") { 
        hidePopup(); 
      } 
    }; 
    

    将上面的示例与其余代码一起检查 here

        2
  •  0
  •   Justin Johnson    16 年前

    试着换一种方式去做。使用三层方法,底层是普通页面,顶层是弹出div,中间层是透明的全屏catch-all div,具有 onfocus 事件关闭弹出窗口(而不是使用 onblur ).

    如果您提供一些代码,将更容易帮助您的方法。