如何完全禁用任何鼠标点击
在用户单击...“登录”按钮和其他事件后,我制作了一个加载脚本 - 让用户知道他们必须等待(直到 ajax 回复)。
如何禁用 div id="doc"
上的任何鼠标点击(右键单击、左键单击、双击、中键单击、x 单击)?
我想将该代码添加到 loading.js
HTML
<html>
...
<body>
<div id="doc">
<div id="content">
...
<input type="button" value="Login" id="login" />
...
</div id="content">
</div id="doc">
</body>
</html>
loading.js
function load_bar(x)
{
if (x==0)
{
$(document.body).css( {"cursor": "default"} );
$("body").css( {"cursor": "default"} );
$("#loading").css("visibility", "hidden"); //modal window
// $("#doc").....ENABLE all clicks (left/right/etc)
}
else if (x==1)
{
$(document.body).css( {"cursor": "wait"} );
$("body").css( {"cursor": "wait"} );
$("#loading").css( {"visibility": "visible"} ); //modal window
// $("#doc").....DISABLE all clicks (left/right/etc)
}
else
{
return alert("Wrong argument!");
}
}
jQuery
$(document).ready(function()
{
//AJAX
$("#login").click(function()
{
load_bar(1); //DISABLE clicks and show load_bar
$(":input").attr("disabled", true);
$.post(
...
function(data)
{
...
load_bar(0); //ENABLE clicks and hide load_bar
...
} //END: if:else
}); //END:$.post
...
}); //END:ajax
}); //END:jQuery
After the user clicks on...."log in" button, and other events, I made a loading script -to let users know they have to wait (Until ajax replies back).
How can I DISABLE any MOUSE CLICKS (right click, left click, double click, middle click, x click), on div id="doc"
?
I want to add that code to loading.js
HTML
<html>
...
<body>
<div id="doc">
<div id="content">
...
<input type="button" value="Login" id="login" />
...
</div id="content">
</div id="doc">
</body>
</html>
loading.js
function load_bar(x)
{
if (x==0)
{
$(document.body).css( {"cursor": "default"} );
$("body").css( {"cursor": "default"} );
$("#loading").css("visibility", "hidden"); //modal window
// $("#doc").....ENABLE all clicks (left/right/etc)
}
else if (x==1)
{
$(document.body).css( {"cursor": "wait"} );
$("body").css( {"cursor": "wait"} );
$("#loading").css( {"visibility": "visible"} ); //modal window
// $("#doc").....DISABLE all clicks (left/right/etc)
}
else
{
return alert("Wrong argument!");
}
}
jQuery
$(document).ready(function()
{
//AJAX
$("#login").click(function()
{
load_bar(1); //DISABLE clicks and show load_bar
$(":input").attr("disabled", true);
$.post(
...
function(data)
{
...
load_bar(0); //ENABLE clicks and hide load_bar
...
} //END: if:else
}); //END:$.post
...
}); //END:ajax
}); //END:jQuery
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在正文或特定 div 中添加简单的 css3 规则,使用
pointer-events: none;
属性。You can add a simple css3 rule in the body or in specific div, use
pointer-events: none;
property.您可以覆盖一个大的半透明
来承受所有点击。只需使用以下样式将新的
附加到
即可:
You can overlay a big, semi-transparent
<div>
that takes all the clicks. Just append a new<div>
to<body>
with this style:要禁用所有鼠标单击
以再次启用它:
To disable all mouse click
to enable it again:
比如:
应该做这份工作
您还可以绑定更多鼠标事件,替换为:
编辑:
在冻结部分添加此内容
,在解冻部分添加此内容:
something like:
should do the job
you could also bind more mouse events with replacing for:
edit:
add this in the feezing part
and this in the unfreezing:
冻结 UI 最简单的方法是使 AJAX 调用同步。
通常,同步 AJAX 调用会破坏使用 AJAX 的目的,因为它会冻结 UI,但如果您想要阻止用户与 UI 交互,那就这么做吧。
The easiest way to freeze the UI would be to make the AJAX call synchronous.
Usually synchronous AJAX calls defeat the purpose of using AJAX because it freezes the UI, but if you want to prevent the user from interacting with the UI, then do it.