Invoke click events on two superimposed, non-hierarchical elements

孤者浪人 提交于 2019-12-11 03:21:25

问题


Elements A and B are not parent and child. Element A is layered (transparently) over the top of Element B. Element B has a button on it.

I want clicking on the button to trigger click events on both A and B.

If B were a child of A (or vice-versa) this would happen automatically (HTML event bubbling JustWorks like that), and event.stopPropagation() would allow me to prevent this if I needed to.

If I wanted the click to pass through the top-most layer (A) and invoke the button on B, then I would use pointer-events:none but that means that NO event is triggered on A.

Can this be achieved? See JSFiddle: https://jsfiddle.net/7mhb5e7c/ I want both events to be fired from a single click.

(For reference, this is not directly my problem, merely my current favourite design for the fix. My actual problem is over here. If you have a better solution that solves the real problem feel free to comment over there. But I'd like to know whether this design is possible for general knowledge, even if it's not the best fix)


回答1:


$('.filter-layer').on('click',function(e) { 
    alert('filter clicked'); 
});
$('button').on('click',function(e) { 
    $('.filter-layer').trigger('click');
    alert('button clicked'); 
});

working example



来源:https://stackoverflow.com/questions/30852940/invoke-click-events-on-two-superimposed-non-hierarchical-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!