最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

html - Javascript -- how to click anywhere on page to hide opened div - Stack Overflow

matteradmin4PV0评论

I have a javascript that opens up a hidden div:

<script>
function dropdown() 
{ document.getElementById("login_dropdown").style.display="block"; }
</script>

The html is:

<div onclick="dropdown()">
<div id="login_dropdown">STUFF</div>
</div>

The CSS is:

<style>
#login_dropdown {
width: 150px;
display:none;
}</style>

Using javascript alone, how can I hide this div when I click anywhere else on the page, excluding the opened DIV itself.

I have a javascript that opens up a hidden div:

<script>
function dropdown() 
{ document.getElementById("login_dropdown").style.display="block"; }
</script>

The html is:

<div onclick="dropdown()">
<div id="login_dropdown">STUFF</div>
</div>

The CSS is:

<style>
#login_dropdown {
width: 150px;
display:none;
}</style>

Using javascript alone, how can I hide this div when I click anywhere else on the page, excluding the opened DIV itself.

Share Improve this question asked Jun 16, 2017 at 18:12 stylusssstylusss 792 silver badges10 bronze badges 2
  • You can set up a click handler on the body element to handle all clicks on the page iirc. Then you just need the handler to hide the element. – Carcigenicate Commented Jun 16, 2017 at 18:16
  • Could someone show me on jFiddle? – stylusss Commented Jun 16, 2017 at 18:30
Add a ment  | 

4 Answers 4

Reset to default 6

Something like this with vanilljs

document.addEventListener('click', function(event){
   const yourContainer = document.querySelector('....');
   if(!yourContainer.contains(event.target)) {
      //hide things classes.. yourContainer.classList.add('hidden');
   }
});

function closest(e, t){ 
  return !e? false : e === t ? true : closest(e.parentNode, t);
}

container = document.getElementById("popup");
open = document.getElementById("open");

open.addEventListener("click", function(e) {
  container.style.display = "block";
  open.disabled = true;
  e.stopPropagation();
});

document.body.addEventListener("click", function(e) {
    if (!closest(e.target, container)) {
        container.style.display = "none";
        open.disabled = false;
    }
});
#popup {
  border: 1px solid #ccc;
  padding: 5px;
  display: none;
  width: 200px;
}
<div id="container">
  <button id="open">open</button>
  <div id="popup">PopUp</div>
</div>

You could do this

var elem = document.getElementById("login_dropdown");
(document.body || document.documentElement).addEventListener('click', function (event) {
  // If the element on which the click event occurs is not the dropdown, then hide it
  if (event.target !== elem)
    elem.style.display="none";
}, false);

Something like this:

$("document").mouseup(function(e)
  {
    var subject = $("#login_dropdown");

    if(e.target.id != subject.attr('id'))
    {
        subject.css('display', 'none');
    }
});

works like this. When you click anywhere on the page, the handler fires and pares the ID of the open tab vs the id of the document (which there is none) - so it closes the tab. However, if you click the tab, the handler fires, checks the ID, sees the target is the same and fails the test (thus not closing the tab).

Post a comment

comment list (0)

  1. No comments so far