diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/element/contextmenu_event/index.html | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/element/contextmenu_event/index.html')
-rw-r--r-- | files/zh-cn/web/api/element/contextmenu_event/index.html | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/element/contextmenu_event/index.html b/files/zh-cn/web/api/element/contextmenu_event/index.html new file mode 100644 index 0000000000..37f3cadd36 --- /dev/null +++ b/files/zh-cn/web/api/element/contextmenu_event/index.html @@ -0,0 +1,101 @@ +--- +title: 'Element: contextmenu event' +slug: Web/API/Element/contextmenu_event +tags: + - API + - DOM + - 上下文 + - 上下文菜单 + - 事件 + - 元素 + - 右击 + - 右键单击 + - 按钮 + - 菜单 + - 鼠标 +translation_of: Web/API/Element/contextmenu_event +--- +<div>{{APIRef}}</div> + +<p><span class="seoSummary"><strong><code>contextmenu</code></strong> 事件会在用户尝试打开上下文菜单时被触发。该事件通常在鼠标点击右键或者按下键盘上的菜单键时被触发,如果使用菜单键,该上下文菜单会被展示 到所聚焦元素的左下角,但是如果该元素是一棵DOM树的话,上下文菜单便会展示在当前这一行的左下角。</span></p> + +<p>任何没有被禁用的鼠标右击事件 (通过调用事件的 {{domxref("Event.preventDefault", "preventDefault()")}} 方法) 将会使得 <code>contextmenu</code> 事件在目标元素上被触发。</p> + +<table class="properties"> + <tbody> + <tr> + <th>Bubbles(冒泡)</th> + <td>Yes</td> + </tr> + <tr> + <th>Cancelable(可撤销)</th> + <td>Yes</td> + </tr> + <tr> + <th>Interface(接口)</th> + <td>{{DOMxRef("MouseEvent")}}</td> + </tr> + <tr> + <th>Event handler property(事件处理器)</th> + <td>{{domxref("GlobalEventHandlers.oncontextmenu", "oncontextmenu")}}</td> + </tr> + </tbody> +</table> + +<h2 id="实例">实例</h2> + +<p>在下面的例子中,第一段内容被触发的 <code>contextmenu</code> 事件的默认行为被 <code>preventDefault()</code> 取消了,因此,在第一段右击鼠标时什么也不会发生,但是右键单击第二段内容时,则会出现标准的菜单内容,与平时右击普通页面出现的菜单内容一致。</p> + +<h3 id="HTML">HTML</h3> + +<pre class="brush: html"><p id="noContextMenu">这个段落右键菜单已被禁用。</p> +<p>但是这个段落没有被禁用。</p></pre> + +<h3 id="JavaScript">JavaScript</h3> + +<pre class="brush: js">noContext = document.getElementById('noContextMenu'); + +noContext.addEventListener('contextmenu', e => { + e.preventDefault(); +}); +</pre> + +<h3 id="Result">Result</h3> + +<p>{{EmbedLiveSample("Examples")}}</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">说明</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{ SpecName('HTML WHATWG', 'indices.html#event-contextmenu', 'contextmenu')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("api.Element.contextmenu_event")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li><a href="/en-US/docs/Learn/JavaScript/Building_blocks/Events">Introduction to events</a></li> + <li>{{event("auxclick")}}</li> + <li>{{event("click")}}</li> + <li>{{event("dblclick")}}</li> + <li>{{event("mousedown")}}</li> + <li>{{event("mouseup")}}</li> + <li>{{event("pointerdown")}}</li> + <li>{{event("pointerup")}}</li> +</ul> |