如何禁用鼠标左键单击?
我想禁用鼠标左键单击。我使用以下脚本但不起作用。
<h:form>
<a4j:commandButton value="TestButton" onclick="alert('button cliked')"/>
</h:form>
JavaScript 是:
<script type="text/javascript">
document.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP | Event.CLICK);
document.onmousedown = clickIE4;
document.onmouseup = clickIE4;
document.onclick = clickIE4;
function clickIE4()
{
return false;
}
</script>
帮助我。 感谢您的努力。
I want to disable mouse left click. I use the following script but not work.
<h:form>
<a4j:commandButton value="TestButton" onclick="alert('button cliked')"/>
</h:form>
JavaScript is :
<script type="text/javascript">
document.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP | Event.CLICK);
document.onmousedown = clickIE4;
document.onmouseup = clickIE4;
document.onclick = clickIE4;
function clickIE4()
{
return false;
}
</script>
Help me.
Thanks for your effort.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的做法是错误的。您想要禁用该按钮(enabled=false),而不是捕获鼠标点击。
You are going about this incorrectly. You want to disable the button (enabled=false), not capture mouse clicks.
使用该样式的处理程序时,从处理程序代码返回
false
:如 剧院说,但是,如果您的目标是禁用该按钮,那么您最好实际上禁用该按钮,而不是阻止点击它。但就您的问题“如何禁用左键单击”而言,这就是答案。
更彻底地说,有两种不同的方法来附加事件处理程序:
DOM0(您使用的样式):从处理程序代码返回
false
以停止事件(防止其冒泡到其他元素)和防止默认操作:DOM2(使用 JavaScript 代码):DOM2 处理程序通过
addEventListener
(或 IE 上的attachEvent
)附加。下面是一个示例,假设一个元素的 id 为“foo”:一些参考资料:
When using that style of handler, return
false
from the handler code:As theatrus said, though, if your goal is to disable the button, you're better off actually disabling the button rather than preventing clicks on it. But in terms of your question "how do I disable a left click," that's the answer.
More thoroughly, there are two different ways to attach event handlers:
DOM0 (the style you've used): Return
false
from the handler code to stop the event (prevent it bubbling up to other elements) and prevent the default action:DOM2 (with JavaScript code): DOM2 handlers are attached via
addEventListener
(orattachEvent
, on IE). Here's an example, assuming an element with theid
"foo":Some reference material: