简体   繁体   English

禁用上下文菜单但不在文本框中

[英]Disable contextmenu but NOT in textboxes

I want to disable the contextmenu on my page, because I have an own.我想禁用我页面上的上下文菜单,因为我有自己的。 But in the textboxes it should be possible to use the system contextmenu.但在文本框中应该可以使用系统上下文菜单。

I'm doing it with Javascript like that:我正在用 Javascript 这样做:

document.oncontextmenu = function (e) {
    return false;
};

I thought about something like:我想过类似的事情:

document.oncontextmenu = function (e) {
    if (e.taget.nodeName != "text") {
    return false;
    }
};

But the nodeName is every time a DIV.但是 nodeName 每次都是 DIV。 In this DIV I have an JQGrid where are the textboxes in.在这个 DIV 中,我有一个 JQGrid,其中包含文本框。

<div id="divGrdPos" style="padding:3px,0px,3px,0px;">
    <table id="JQGridCart" class="grdCart"></table>
</div>

Can someone help me?有人能帮我吗?

You need something like:你需要这样的东西:

document.addEventListener('contextmenu', function (event) {
    if (event.target.nodeName !== 'INPUT' && event.target.type !== 'text' && event.target.nodeName !== 'TEXTAREA') {
        event.preventDefault();
    }
});

It will work for inputs (type="text") and textareas.它适用于输入 (type="text") 和文本区域。

Jsfiddle: http://jsfiddle.net/QjmHy/ Jsfiddle:http: //jsfiddle.net/QjmHy/

Why are you checking against text textbox nodeName is INPUT你为什么要检查textnodeName is INPUT

document.oncontextmenu = function (e) {
    console.log(e);
    if (e.target.nodeName != "INPUT") {
        return false;
    }
};​

http://jsfiddle.net/qr3fu/ http://jsfiddle.net/qr3fu/

I found the mistake.我发现了错误。 In my code I have a mousedown() event on my grid and this event fires before the oncontextmenu() event.在我的代码中,我的网格上有一个mousedown()事件,该事件在oncontextmenu()事件之前触发。 and my mousedown() is greating my own contextmenu with a div after checking the right click.在检查右键单击后,我的mousedown()正在用一个 div 来丰富我自己的上下文菜单。

So I checked in my mousedown() event if the nodeName is "input" and handle the result.所以我检查了我的mousedown()事件,如果nodeName"input"并处理结果。

the oncontextmenu() will be the same. oncontextmenu()将是相同的。

thx for your tips.谢谢你的提示。

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

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