简体   繁体   English

如果模式可见,则阻止任何点击

[英]block any click if modal is visible

<body>
<div id='container'>
<div id='cpanel' style="position:fixed;">525</div>
</div>
<div class='modal' style="position:fixed;>323</div>
</body>

I want to prevent any click on container and its children if modal is visible. 如果modal可见,我想防止对container及其子级的任何单击。

js js

$('#container, #cpanel').click(function(){
    if ($('.modal').is(':visible')) {return false;}
});

Click on container is blocked, but on cpanel is not ! 单击container被阻止,但单击cpanel则不!

Any help? 有什么帮助吗?

A possible solution can consider the CSS pointer-event property. 可能的解决方案可以考虑CSS 指针事件属性。

If you are using the jquery ui dialog you can add the code to the events: 如果使用的是jQuery ui对话框,则可以将代码添加到事件中:

 $('.modal').dialog({ open: function( event, ui ) { $('#cpanel').css('pointer-events','none'); }, close: function( event, ui ) { $('#cpanel').css('pointer-events','auto'); }, }); $('#container, #cpanel').click(function(e){ e.stopPropagation(); console.log('--->clicked'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script> <div id='container'> <div id='cpanel' style="position:fixed;">525</div> </div> <div class='modal' style="position:fixed;">323</div> 

Try stopPropagation method: 尝试stopPropagation方法:

$('#container, #cpanel').on("click", function(event){
    if ($('.modal').is(':visible')) {
       event.stopPropagation()
    }
});

https://codepen.io/anon/pen/zjywXV https://codepen.io/anon/pen/zjywXV

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM