javascript 如何禁用页面上的所有 onclick 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15801713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I disable all the onclick events on a page
提问by Mojtaba
Hi want to disable all onclick events on a page i write this:
嗨想禁用页面上的所有 onclick 事件我写这个:
window.onclick = anyfunction;
document.onclick=anyfunction;
function anyfunction() {};
That's work but if page contain this code
这是工作,但如果页面包含此代码
$("body").click(function () {
anyfunctions();
});
It will run. even i add
它会运行。即使我添加
document.body.onclick
document.body.onclick
Jquery code runs again. I want to disable all onclick functions (java script and jquery and ... ) with javascript without using jquery library
Jquery 代码再次运行。我想在不使用 jquery 库的情况下使用 javascript 禁用所有 onclick 函数(java 脚本和 jquery 和 ...)
Target is to block popup windows Thanks
目标是阻止弹出窗口 谢谢
回答by Impirator
$("*").unbind("click"); // Removes all click handlers added by javascript from every element
$("[onclick]").removeAttr("onclick"); // Finds all elements with an 'onclick' attribute, and removes that attribute
回答by Mounika Soma
This css may help you in a better way.
这个 css 可以更好地帮助你。
function disableEvents()
{
$('#divname').css('pointer-events','none');
}
call this function where ever and whenever you would like to stop the click events on your page. This will work.
无论何时何地,只要您想停止页面上的点击事件,都可以调用此函数。这将起作用。
回答by lukeocom
you could also try
你也可以试试
$('*').click(function(e){e.preventDefault();});
also see - How to prevent onclick statements from being executed?
另请参阅 -如何防止执行 onclick 语句?