我有一个具有背景的div:透明,以及边框。在这个div下面,我有更多的元素。
目前,当我在覆盖div外部单击时,我可以单击底层元素。但是,当我直接单击覆盖div时,我无法单击底层元素。
我希望能够单击这个div,以便单击底层元素。
我有一个具有背景的div:透明,以及边框。在这个div下面,我有更多的元素。
目前,当我在覆盖div外部单击时,我可以单击底层元素。但是,当我直接单击覆盖div时,我无法单击底层元素。
我希望能够单击这个div,以便单击底层元素。
当前回答
我需要这样做,并决定走这条路:
$('.overlay').click(function(e){
var left = $(window).scrollLeft();
var top = $(window).scrollTop();
//hide the overlay for now so the document can find the underlying elements
$(this).css('display','none');
//use the current scroll position to deduct from the click position
$(document.elementFromPoint(e.pageX-left, e.pageY-top)).click();
//show the overlay again
$(this).css('display','block');
});
其他回答
我认为event.stopPropagation();这里也应提及。将此添加到按钮的单击功能。
防止事件在DOM树中冒泡,防止任何父处理程序收到事件通知。
不,你不能点击一个元素。您可以获取单击的坐标,并尝试找出单击元素下面是什么元素,但对于没有document.elementFromPoint的浏览器来说,这真的很乏味。然后,您仍然需要模拟默认的单击操作,这不一定是微不足道的,这取决于您在那里有什么元素。
由于你有一个完全透明的窗口区域,你可能会更好地将其作为外部的独立边界元素来实现,让中心区域没有障碍物,这样你就可以直接点击。
我需要这样做,并决定走这条路:
$('.overlay').click(function(e){
var left = $(window).scrollLeft();
var top = $(window).scrollTop();
//hide the overlay for now so the document can find the underlying elements
$(this).css('display','none');
//use the current scroll position to deduct from the click position
$(document.elementFromPoint(e.pageX-left, e.pageY-top)).click();
//show the overlay again
$(this).css('display','block');
});
隐藏覆盖元素确定光标坐标获取这些坐标上的元素触发点击元素再次显示覆盖元素
$('#elementontop').click(e => {
$('#elementontop').hide();
$(document.elementFromPoint(e.clientX, e.clientY)).trigger("click");
$('#elementontop').show();
});
另一个尝试(情境)的想法是:
将所需内容放入div中;将非点击覆盖放在整个页面上,z索引更高,制作原始div的另一个裁剪副本overlay和abs将复制div放置在与原始内容相同的位置,您希望使用更高的z索引进行单击?
有什么想法吗?