如何禁用鼠标左键单击?

发布于 2024-09-07 02:13:08 字数 571 浏览 4 评论 0原文

我想禁用鼠标左键单击。我使用以下脚本但不起作用。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老子叫无熙 2024-09-14 02:13:08

你的做法是错误的。您想要禁用该按钮(enabled=false),而不是捕获鼠标点击。

You are going about this incorrectly. You want to disable the button (enabled=false), not capture mouse clicks.

奈何桥上唱咆哮 2024-09-14 02:13:08

使用该样式的处理程序时,从处理程序代码返回 false

<a4j:commandButton value="TestButton" onclick="alert('button clicked'); return false;"/>

剧院说,但是,如果您的目标是禁用该按钮,那么您最好实际上禁用该按钮,而不是阻止点击它。但就您的问题“如何禁用左键单击”而言,这就是答案。

更彻底地说,有两种不同的方法来附加事件处理程序:

DOM0(您使用的样式):从处理程序代码返回 false 以停止事件(防止其冒泡到其他元素)和防止默认操作:

<someElement onclick="return false;">

DOM2(使用 JavaScript 代码):DOM2 处理程序通过 addEventListener(或 IE 上的 attachEvent)附加。下面是一个示例,假设一个元素的 id 为“foo”:

// Attaching:
var elm = document.getElementById("foo");
if (elm.attachEvent) {
    elm.attachEvent("onclick", handler);
}
else if (elm.addEventListener) {
    elm.addEventListener("click", handler, false);
}

// Handler:
function handler(event) {
    // DOM standard is that `event` is an argument to the
    // handler; IE uses a global event object instead.
    // This line handles that.
    event = event || window.event;

    // Cancelling bubbling:
    if (event.stopPropagation) {
        // DOM standard
        event.stopPropagation();
    }
    else {
        // Older mechanism
        event.cancelBubble = true;
    }

    // Preventing default action:
    if (event.preventDefault) {
        // DOM standard
        event.preventDefault();
    }
    else {
        // Older mechanism
        event.returnValue = false;
    }
}

一些参考资料:

When using that style of handler, return false from the handler code:

<a4j:commandButton value="TestButton" onclick="alert('button clicked'); return false;"/>

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:

<someElement onclick="return false;">

DOM2 (with JavaScript code): DOM2 handlers are attached via addEventListener (or attachEvent, on IE). Here's an example, assuming an element with the id "foo":

// Attaching:
var elm = document.getElementById("foo");
if (elm.attachEvent) {
    elm.attachEvent("onclick", handler);
}
else if (elm.addEventListener) {
    elm.addEventListener("click", handler, false);
}

// Handler:
function handler(event) {
    // DOM standard is that `event` is an argument to the
    // handler; IE uses a global event object instead.
    // This line handles that.
    event = event || window.event;

    // Cancelling bubbling:
    if (event.stopPropagation) {
        // DOM standard
        event.stopPropagation();
    }
    else {
        // Older mechanism
        event.cancelBubble = true;
    }

    // Preventing default action:
    if (event.preventDefault) {
        // DOM standard
        event.preventDefault();
    }
    else {
        // Older mechanism
        event.returnValue = false;
    }
}

Some reference material:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文