附上LxcJie(肖冲)的回复:<SCRIPT language="javascript">
document.onclick = function()
{
    if(event.srcElement.tagName.toLowerCase() != "div")
    {
        if(window.preObj == myDiv)
        {
            myFunc();
            window.preObj = null;
        }
    }
}function myFunc()
{
    alert("调用myFunc()");
    myDiv.style.display = "none";
}
</SCRIPT>
<BODY>
<DIV id="myDiv" style="width:100px; height:100px; background-color:#CCCCCC; 
     border:1px solid black" onClick="window.preObj = this">
</DIV>
</BODY>这样当点击DIV内的其他元素时仍会触发myFunc函数,这样是不行的,需要判断元素是否是DIV的子元素或者判断焦点是否在DIV外才行啊

解决方案 »

  1.   

    <DIV id="myDiv" style="width:100px; height:100px; background-color:#CCCCCC; 
         border:1px solid black" onClick="window.preObj = this">
         <input type=text value="ddd"/>
         <table border="1">
          <tr>
          <td>ddddd</td>
          </tr>
         </table>
    </DIV>
      

  2.   

    <SCRIPT language="javascript">
    document.onclick = function()
    {
        if(getElement(event.srcElement) == null)
        {
            if(window.preObj == myDiv)
            {
                myFunc();
                window.preObj = null;
            }
        }
    }function getElement(obj)
    {
        while(obj)
        {
            if(obj == document.all.myDiv)
                return document.all.myDiv;
            obj = obj.parentNode;
        }
        return null;
    }function myFunc()
    {
        alert("myFunc()");
        myDiv.style.display = "none";
    }
    </SCRIPT>
    <BODY>
    <DIV id="myDiv" style="width:100px; height:100px; background-color:#CCCCCC; 
         border:1px solid black" onClick="window.preObj = this">
    <input type=text value="ddd"/>
    <table border="1">
    <tr>
        <td>ddddd</td>
    </tr>
    </table>
    </DIV>
    </BODY>
      

  3.   

    这下可以了,非常感谢LxcJie(肖冲)