You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<desc>Bind an event handler to the "contextmenu" event, or trigger that event on an element.</desc>
2
+
<entries>
3
+
<desc>Bind an event handler to the "contextmenu" event, or trigger that event on an element.</desc>
4
+
5
+
<entrytype="method"name="on"return="jQuery">
6
+
<title>contextmenu event</title>
7
+
<desc>Bind an event handler to the "contextmenu" event.</desc>
5
8
<signature>
6
9
<added>1.0</added>
7
10
<argumentname="handler"type="Function">
@@ -19,12 +22,8 @@
19
22
<argumentname="eventObject"type="Event" />
20
23
</argument>
21
24
</signature>
22
-
<signature>
23
-
<added>1.0</added>
24
-
</signature>
25
25
<longdesc>
26
-
<p>This method is a shortcut for <code>.on( "contextmenu", handler )</code> in the first two variations, and <code>.trigger( "contextmenu" )</code> in the third.
27
-
The <code>contextmenu</code> event is sent to an element when the right button of the mouse is clicked on it, but before the context menu is displayed. In case the context menu key is pressed, the event is triggered on the <code>html</code> element or the currently focused element. Any HTML element can receive this event.
26
+
<p>The <code>contextmenu</code> event is sent to an element when the right button of the mouse is clicked on it, but before the context menu is displayed. In case the context menu key is pressed, the event is triggered on the <code>html</code> element or the currently focused element. Any HTML element can receive this event.
28
27
For example, consider the HTML:</p>
29
28
<pre><code>
30
29
<div id="target">
@@ -33,34 +32,34 @@
33
32
</code></pre>
34
33
<p>The event handler can be bound to the <code><div></code> as follows:</p>
35
34
<pre><code>
36
-
$( "#target" ).contextmenu(function() {
37
-
alert( "Handler for .contextmenu() called." );
38
-
});
35
+
$( "#target" ).on( "contextmenu", function() {
36
+
alert( "Handler for `contextmenu` called." );
37
+
});
39
38
</code></pre>
40
39
<p>Now right-clicking on this element displays the alert:</p>
41
40
<p>
42
-
<samp>Handler for .contextmenu() called.</samp>
41
+
<samp>Handler for `contextmenu` called.</samp>
43
42
</p>
44
-
<p>To trigger the event manually, call <code>.contextmenu()</code> without an argument:</p>
43
+
<p>To trigger the event manually, use <code>.trigger( "contextmenu" )</code>:</p>
45
44
<pre><code>
46
-
$( "#target" ).contextmenu();
45
+
$( "#target" ).trigger( "contextmenu" );
47
46
</code></pre>
48
47
</longdesc>
49
48
<example>
50
49
<desc>To show a "Hello World!" alert box when the contextmenu event is triggered on a paragraph on the page:</desc>
51
50
<code><![CDATA[
52
-
$( "p" ).contextmenu(function() {
51
+
$( "p" ).on( "contextmenu", function() {
53
52
alert( "Hello World!" );
54
-
});
53
+
});
55
54
]]></code>
56
55
</example>
57
56
<example>
58
57
<desc>Right click to toggle background color.</desc>
59
58
<code><![CDATA[
60
59
var div = $( "div" ).first();
61
-
div.contextmenu(function() {
60
+
div.on( "contextmenu", function() {
62
61
div.toggleClass( "contextmenu" );
63
-
});
62
+
});
64
63
]]></code>
65
64
<css><![CDATA[
66
65
div {
@@ -81,5 +80,23 @@ div.contextmenu(function() {
81
80
</example>
82
81
<categoryslug="events/mouse-events"/>
83
82
<categoryslug="version/1.0"/>
84
-
<categoryslug="version/1.4.3"/>
83
+
<categoryslug="version/1.7"/>
84
+
</entry>
85
+
86
+
<entrytype="method"name="trigger"return="jQuery">
87
+
<title>contextmenu event</title>
88
+
<desc>Trigger the "contextmenu" event on an element.</desc>
<desc>An object containing data that will be passed to the event handler.</desc>
15
15
</argument>
@@ -18,12 +18,7 @@
18
18
<argumentname="eventObject"type="Event" />
19
19
</argument>
20
20
</signature>
21
-
<signature>
22
-
<added>1.0</added>
23
-
</signature>
24
-
<desc>Bind an event handler to the "keydown" event, or trigger that event on an element.</desc>
25
21
<longdesc>
26
-
<p>This method is a shortcut for <code>.on( "keydown", handler )</code> in the first and second variations, and <code>.trigger( "keydown" )</code> in the third.</p>
27
22
<p>The <code>keydown</code> event is sent to an element when the user presses a key on the keyboard. If the key is kept pressed, the event is sent every time the operating system repeats the key. It can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.</p>
28
23
<p>For example, consider the HTML:</p>
29
24
<pre><code>
@@ -36,19 +31,19 @@
36
31
</code></pre>
37
32
<p>The event handler can be bound to the input field:</p>
38
33
<pre><code>
39
-
$( "#target" ).keydown(function() {
40
-
alert( "Handler for .keydown() called." );
41
-
});
34
+
$( "#target" ).on( "keydown", function() {
35
+
alert( "Handler for `keydown` called." );
36
+
});
42
37
</code></pre>
43
38
<p>Now when the insertion point is inside the field, pressing a key displays the alert:</p>
44
39
<p>
45
-
<samp>Handler for .keydown() called.</samp>
40
+
<samp>Handler for `keydown` called.</samp>
46
41
</p>
47
-
<p>To trigger the event manually, apply <code>.keydown()</code> without an argument:</p>
42
+
<p>To trigger the event manually, use <code>.trigger( "keydown" )</code>:</p>
48
43
<pre><code>
49
-
$( "#other" ).click(function() {
50
-
$( "#target" ).keydown();
51
-
});
44
+
$( "#other" ).on( "click", function() {
45
+
$( "#target" ).trigger( "keydown" );
46
+
});
52
47
</code></pre>
53
48
<p>After this code executes, clicks on <samp>Trigger the handler</samp> will also alert the message.</p>
54
49
<p>If key presses anywhere need to be caught (for example, to implement global shortcut keys on a page), it is useful to attach this behavior to the <code>document</code> object. Because of event bubbling, all key presses will make their way up the DOM to the <code>document</code> object unless explicitly stopped.</p>
0 commit comments