我有一个主要的div和子div也被称为形式,因为表单被包裹在div内。我想要的是在主div之外单击时关闭窗体,但完全没有发生。请帮我解决这个问题。Event.target无法正常工作,当单击关闭它的外部
var btn = document.getElementById('opener');
var box = document.getElementById('abc');
var form = document.getElementById('def');
btn.onclick = function(){
\t box.style.display = "block";
}
//Doesn't work. It's function is to close the form when click outside of the div.
window.onclick = function(event){
\t if(event.target == box){
\t form.style.display = "none";
}
}
#abc{
display: none;
background-color: #F44336;
}
\t <button id = "opener">
Open
</button>
<div id = "abc">
\t \t <!-- Login Authentication -->
\t \t <div id = "def">
\t \t \t <div>
\t \t \t \t <p>Welcome</p>
\t \t \t </div>
\t \t \t <br />
\t \t \t <div class = "login-auth" id = "cool">
\t \t \t \t \t <form method="POST">
\t \t <label>Email or Username:</label>
\t \t <input type = "text">
\t \t <br />
\t \t <label>Password:</label>
\t \t <input type = "password">
\t \t <br /><br />
\t \t <input type="submit" value="Login">
\t </form>
\t \t \t </div>
\t \t </div>
\t </div>
的jsfiddle链接在这里:https://jsfiddle.net/twrvpp3d/
如果你简单地把'的console.log(event.target);'单击处理程序,你会看到这个问题。 – Barmar
看看 - > https://jsfiddle.net/twrvpp3d/9/ – UncaughtTypeError
感谢@UncaughtTypeError的答案。但问题已经解决了,虽然:d – Sanjay