为了选择或聚焦对象,如何使右键单击的行为与单击鼠标左键的行为相同 [英] How can I make a right-click behave as a left-click for the purpose of selecting or focusing an object

查看:97
本文介绍了为了选择或聚焦对象,如何使右键单击的行为与单击鼠标左键的行为相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事件,尽管按鼠标右键(据我所知)会触发mousedown事件,但在大多数情况下,mousedown事件似乎被忽略了.我目前正在通过右键单击显示自定义上下文菜单,但是我还希望能够在右键单击时从列表中选择一个选项.截至目前,我认识到两个按钮的单击足以运行与onmousedown属性关联的一些javascript,但不足以选择当鼠标按下来自右键时鼠标悬停的选项.

Event though a right-click (as far as I know) fires a mousedown event, that mousedown seems to be ignored in most cases. I'm currently working on displaying a custom context menu via a right-click, but I'd also like to be able to select an option from a list while I'm right-clicking. As of right now my recognizes the click from both buttons enough to run some javascript tied to the onmousedown attribute but not enough to select the option the mouse is over when the mousedown comes from the right button.

是否有一种方法可以绕过浏览器的默认行为,即忽略鼠标右键单击的mousedown事件,或者愚弄它以为鼠标按下是由左按钮生成的?

Is there a way to bypass a browser's default behavior of ignoring the mousedown event of a right-click or fool it into thinking the mousedown was generated by the left button instead?

谢谢.

推荐答案

您可以使用

You can use the oncontextmenu event.

编辑:要在鼠标右键单击过程中模拟<option>的默认单击行为,请在处理右键单击时将此代码放在事件处理程序中:

To simulate the default click behavior on an <option> during a right mouse click, put this code in your event handler when handling right click:

clickedOption.parentNode.selectedIndex = clickedOption.index;

这篇关于为了选择或聚焦对象,如何使右键单击的行为与单击鼠标左键的行为相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆