最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Prevent onClick from firing if another element is on top - Stack Overflow

programmeradmin7浏览0评论

I have an app in which something happens when you click the background, however, I don't want it to activate if you click on something else that is on top of the background. For example:

function handleClick() {
  alert("Hello!");
}
.container {
  position: absolute;
  height: 100%;
  width: 100%;
  background: red;
}

.button {
  position: absolute;
  height: 20%;
  width: 20%;
  background: green;
}
<div class="container" onClick="handleClick()">
  <div class="button"></div>
</div>

I have an app in which something happens when you click the background, however, I don't want it to activate if you click on something else that is on top of the background. For example:

function handleClick() {
  alert("Hello!");
}
.container {
  position: absolute;
  height: 100%;
  width: 100%;
  background: red;
}

.button {
  position: absolute;
  height: 20%;
  width: 20%;
  background: green;
}
<div class="container" onClick="handleClick()">
  <div class="button"></div>
</div>

In this example, how can I prevent the alert from displaying when the user clicks on the green box?

Share Improve this question edited Apr 3, 2018 at 18:43 JakAttk123 asked Apr 3, 2018 at 18:40 JakAttk123JakAttk123 1,8063 gold badges12 silver badges20 bronze badges 1
  • Duplicate of stackoverflow./questions/41000403/… – CInvt Commented Apr 3, 2018 at 18:49
Add a ment  | 

5 Answers 5

Reset to default 7

Pass the event to the function, and check whether its target is the same as the element itself. When you click on an inner element, the target will be that element. Use event.stopPropagation() to prevent the event from bubbling out to the container.

function handleClick(event, element) {
  if (event.target != element) {
    event.stopPropagation();
    return;
  }
  alert("Hello!");
}
.container {
  position: absolute;
  height: 100%;
  width: 100%;
  background: red;
}

.button {
  position: absolute;
  height: 20%;
  width: 20%;
  background: green;
}
<div class="container" onClick="handleClick(event, this)">
  <div class="button"></div>
</div>

You need to use inside div onclick="event.stopPropagation()"

Stopping Bubbling

A bubbling event goes from the target element straight up. Normally it goes upwards till <html>, and then to document object, and some events even reach window, calling all handlers on the path.

But any handler may decide that the event has been fully processed and stop the bubbling.

The method for it is event.stopPropagation().

DEMO

function handleClick() {
  alert("Hello!");
}
.container {
  position: absolute;
  height: 100%;
  width: 100%;
  background: red;
}

.button {
  position: absolute;
  height: 20%;
  width: 20%;
  background: green;
}
<div class="container" onClick="handleClick()">
  <div onclick="event.stopPropagation()" class="button"></div>
</div>

You can pare e.target against e.currentTarget like this:

function handleClick(e) {
  if (e.target == e.currentTarget) {
    alert("Hello!");
  }
}
.container {
  position: absolute;
  height: 100%;
  width: 100%;
  background: red;
}

.button {
  position: absolute;
  height: 20%;
  width: 20%;
  background: green;
}
<div class="container" onClick="handleClick(event)">
  <div class="button"></div>
</div>

I'd detach the event handler when that "something" appears over the top:

element.removeEventListener("mousedown", handleMouseDown, true);

(Note the last argument must match the "useCapture" value when you created the event.)

or in your case:

document.getElementById('yourDiv').onclick = null;

or

document.getElementById("yourDiv").removeAttribute("onClick");

You have added the handleClick function on the div that (class="container") covered the screen. when you click anywhere handleClick will run. So add that function the div that has class= "button"

<div class="container">
  <div class="button" onClick="handleClick()">
</div>
发布评论

评论列表(0)

  1. 暂无评论