admin管理员组

文章数量:1022944

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:

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. In this DIV I have an JQGrid where are the textboxes in.

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

Can someone help me?

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:

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. In this DIV I have an JQGrid where are the textboxes in.

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

Can someone help me?

Share Improve this question edited May 18, 2021 at 6:17 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 19, 2012 at 6:54 SenniSenni 831 silver badge6 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

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.

Jsfiddle: http://jsfiddle/QjmHy/

Why are you checking against text textbox nodeName is INPUT

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

http://jsfiddle/qr3fu/

I found the mistake. In my code I have a mousedown() event on my grid and this event fires before the oncontextmenu() event. and my mousedown() is greating my own contextmenu with a div after checking the right click.

So I checked in my mousedown() event if the nodeName is "input" and handle the result.

the oncontextmenu() will be the same.

thx for your tips.

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:

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. In this DIV I have an JQGrid where are the textboxes in.

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

Can someone help me?

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:

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. In this DIV I have an JQGrid where are the textboxes in.

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

Can someone help me?

Share Improve this question edited May 18, 2021 at 6:17 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 19, 2012 at 6:54 SenniSenni 831 silver badge6 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

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.

Jsfiddle: http://jsfiddle/QjmHy/

Why are you checking against text textbox nodeName is INPUT

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

http://jsfiddle/qr3fu/

I found the mistake. In my code I have a mousedown() event on my grid and this event fires before the oncontextmenu() event. and my mousedown() is greating my own contextmenu with a div after checking the right click.

So I checked in my mousedown() event if the nodeName is "input" and handle the result.

the oncontextmenu() will be the same.

thx for your tips.

本文标签: javascriptDisable contextmenu but NOT in textboxesStack Overflow