简体   繁体   中英

How to hide form by mouse click beyond the form area withou JQuery

I've the follwoing form:

<form method="get">
<!-- This is form's content -->
</form>

I want to hide the form by mouse click beyond the form area. I'm trying to set window.onclick event handler but this handler is override others onlcick handler on my page. I need to do this WITHOUT JQUERY . Thanks.

Give your form an Id and add use e.target.id in addEventListener :

<form method="get" id="myform">
<!-- This is form's content -->
Dummy content
</form>
<script>
    window.onclick = function(e) {
        alert('other handlers');
    }
    window.addEventListener('click', function(e) {
        if (e.target.id != "myform") {
            document.getElementById('myform').style.display = 'none';
        }
    });
</script>

Working demo: http://jsfiddle.net/6bewY/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM