我有一种情况,当我右键单击类types_list_button的div时需要触发一个事件,但是在该div中是另一个类hover_point的div,我不希望事件在单击时触发在那个div上。这可能吗?

我不确定我是否可以对正在使用的这种类型的事件处理程序使用:not

的HTML

<div class="types_list_button">Any where here<div class="hover_point">Not Here</div>Any where here</div>


JQUERY

$(document).on('contextmenu', '.types_list_button', function() {




$(document).on('contextmenu', '.types_list_button', function() {
  alert("test")
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="types_list_button">Any where here<div class="hover_point">Not Here</div>Any where here</div>

最佳答案

只需在事件处理程序内的target of the Event object中检查类:



$(document).on("contextmenu", ".types_list_button", function(evt) {
  if (!$(evt.target).hasClass("hover_point"))
    alert("test");
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="types_list_button" style="margin:1em; padding:1em; background:#fcc;">Anywhere here
  <div class="hover_point" style="margin:1em; padding:1em; background:#cfc;">Not Here</div>
Anywhere here</div>

关于javascript - 右键单击(contextmenu)在div类上,但不在子div类上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48370780/

10-14 13:52
查看更多