【问题标题】:jQuery - Trigger click even on element behindjQuery - 在后面的元素上触发点击事件
【发布时间】:2012-08-08 11:42:35
【问题描述】:

我的问题是我有这个盒子,也就是容器。在该容器内有用户可以单击的框。

为了在视觉上帮助用户,我制作了带有灰色淡出颜色的叠加框 这告诉他们他们可以使用这里的盒子。

但我的问题是点击事件在覆盖框后面的框上。

那么有什么方法可以忽略元素的.click() 并使用下一个目标?

【问题讨论】:

  • 你不能只使用选择器来定位小盒子吗?
  • 当您可以将“已禁用”类分配给实际框以将其设置为已禁用元素的样式时,我不理解您对覆盖框的需求。这样你的点击功能就会畅通无阻。

标签: javascript jquery


【解决方案1】:

您可以使用 CSS pointer-events 属性:

CSS 属性pointer-events 允许作者控制特定图形元素在什么情况下(如果有)可以成为鼠标事件的目标。

#element {
  pointer-events: none;
}

或者你可以triggerclick事件:

$('#box').click(function(){
   $('#target').trigger('click')
})

【讨论】:

  • 小心!我想你会发现 IE 不支持 pointer-events - 例如这里的 see
  • @Beetroot-Beetroot 是的,ie8 不支持pointer-events 属性。
  • 第二篇帖子here 表明pointer-events 目前对于所有非SVG 元素都不可靠,即使在moz 浏览器中也是如此。一方面,我看不到自己很快就会使用它,除非可能用于 SVG 工作。
【解决方案2】:

避免使用 CSS pointer-events ...

首先,确保包含的盒子都具有类名box(或类似名称)。

其次,使用.addClass('inactive') 使框处于非活动状态(在样式表中使用相应的 CSS 指令来处理背景颜色和边框)。要重新激活盒子,只需.removeClass('inactive')

第三,将点击框的处理委托给容器,如下所示:

$('#container').on('click', '.box', function() {
    //common behaviour (pre)
    if($(this).hasClass("inactive")) {
        //inactive box behaviour
    }
    else {
        //active box behaviour
    }
    //common behaviour (post)
});

【讨论】:

  • 我遇到的问题是我正在使用 JavaScript(jQuery) 的 fullcalendar 插件。所以我做了一个自定义,甚至使盒子的宽度为 100%,而不是盒子原始宽度的较小百分比。即使我一直在寻找一种方法,将点击事件从我的框元素重定向到它后面的元素,以在我一无所获时触发点击。所以现在我想指针事件是我唯一的选择,直到我幸运并在全日历源中找到我需要的部分。不过还是谢谢 :)
  • @danniehansenweb,您似乎在描述“事件冒泡”,这正是上面 $('#container').on('click', '.box', function() {...}); 形式的 jQuery 语句所利用的内容。我确信这或类似的东西是前进的方向,但如果没有看到您的 HTML 就无法提供更多建议。
  • 您能否详细说明为什么要避免使用pointer-events?对它的支持似乎相当成熟。
【解决方案3】:

您可以使用由Ivan Castellanos 开发的立即调用函数表达式 (IIFE),它将检测您的鼠标指针在 (x,y) 的位置,并在您选择的元素上评估为真或假,如果事实上,鼠标在那个元素上。

通过使用 Ivan 的代码,我能够生成这个概念证明,它会产生与 pointer-events: none; 相同的结果,但跨浏览器的兼容性稍好一些。它使用 jQuery .offset() 方法。在 jsFiddle 中,有一个包含 3 个元素的页面,两个 <button> 和一个 <div> 覆盖在按钮顶部,不透明度为 50%。在正常情况下,如果不调整 z-index,您将无法单击这些元素。

jsFiddle Example

//CSS Hitbox Solution 08-26-2015
//StackOverflow - https://stackoverflow.com/questions/32233084/show-an-element-without-hitbox-does-not-take-mouse-touch-input
//Detect MouseOver https://stackoverflow.com/questions/1273566/how-do-i-check-if-the-mouse-is-over-an-element-in-jquery
//Source: https://stackoverflow.com/questions/3942776/using-jquery-to-find-an-element-at-a-particular-position
//https://css-tricks.com/snippets/jquery/get-x-y-mouse-coordinates/
(function($) {
  $.mlp = {
    x: 0,
    y: 0
  }; // Mouse Last Position
  function documentHandler() {
    var $current = this === document ? $(this) : $(this).contents();
    $current.mousemove(function(e) {
      jQuery.mlp = {
        x: e.pageX,
        y: e.pageY
      };
    });
    $current.find("iframe").load(documentHandler);
  }
  $(documentHandler);
  $.fn.ismouseover = function(overThis) {
    var result = false;
    this.eq(0).each(function() {
      var $current = $(this).is("iframe") ? $(this).contents().find("body") : $(this);
      var offset = $current.offset();
      result = offset.left <= $.mlp.x && offset.left + $current.outerWidth() > $.mlp.x && offset.top <= $.mlp.y && offset.top + $current.outerHeight() > $.mlp.y;
    });
    return result;
  };
})(jQuery);
$('.notification-box').on("click", function() {
  $("button").each(function(i) {
    var iteratedButton = $('button:eq(' + i + ')');
    var buttonID = iteratedButton.attr("id");
    if (iteratedButton.ismouseover()) {
      iteratedButton.toggleClass(buttonID);
    }
  });
});
.notification-box {
  position: absolute;
  height: 100vw;
  width: 100vw;
  background-color: black;
  opacity: 0.5;
  /*pointer-events: none;*/
  z-index: -10;
  overflow: visible;
}
button {
  position: absolute;
  top: absolute;
  width: 50px;
  z-index: -10;
}
button#no {
  margin-top: 25px;
}
.yes {
  color: red;
  font-weight: bold;
}
.no {
  color: blue;
  font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<button id='yes'>Yes</button>
<button id='no'>No</button>
<div id='no' class="notification-box" />

【讨论】:

    猜你喜欢
    • 2013-07-11
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多