将 javascript 属性事件转发到不同的处理程序/类型
我目前正在尝试为触摸屏计算机设置一个网页,并且注意到(用我的胖手指)onclick 事件确实很难触发,因为我手指的表面积通常会导致光标移动,而我“点击”。
我想使用的解决方案是将 onmousedown 事件转发到 onclick。我怎样才能做到这一点?
最好的答案是:(
<input type="radio" onmousedown="this.onclick()"/>
但当然是在工作条件下)
编辑:我的具体示例是单选按钮 - 当一个被选中时,其他按钮未被选中;当浏览器免费提供给我时,我不想在自定义事件中复制此行为。
注意:编辑并收到反馈后,我认为也许我可能走错了路......也许实际上不是单选按钮上的“单击”功能来进行选择:欢迎新建议......
I'm currently trying to set up a web page for a touch screen computer, and have noticed that (with my fat fingers) that onclick events are really hard to trigger, as the surface area of my finger generally causes the cursor to move while I "click".
The solution I want to use is forwarding onmousedown events to onclick. How can I make this work?
The best answer would be along the lines of:
<input type="radio" onmousedown="this.onclick()"/>
(but of course in a working condition)
EDIT: My specific example is for radio buttons - when one is checked the others are unchecked; I don't want to have to replicate this behaviour in a custom event when the browser gives it to me for free.
NOTE: Having edited, and received feedback, I think that perhaps I might be on the wrong track... perhaps it's not the "click" function on a radio button that actually does the selection: new suggestions welcome ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好的,编辑之后就变得更清楚了 - 您想要做的是在触发
mousedown
事件时模拟用户单击控件。两个选项:
onmouseout="this.checked = true;"
,即单击单选按钮“检查”/选择单选按钮。Ok, after your edits it becomes clearer - what you want to do is to simulate a user clicking the control when the
mousedown
event is fired.Two options:
onmouseout="this.checked = true;"
, i.e. clicking the radio button "checks"/selects the radio button.为什么不直接处理
mousedown
事件呢?Why not just handle the
mousedown
event?我知道你在问题中没有提到它,但如果你碰巧使用 jQuery,那就真的很简单:
I know you didn't mention it in the question, but if you happen to be using jQuery, it'd be really really easy:
也许这对您有用
此外,您可以将输入按钮/字段的样式设置得更大一些,以便通过指针更好地访问。
Maybe this works for you
Additionally you could style your input buttons/fields to be a little bigger, for better accessibility via the pointer.
最好有一个函数来处理这两个事件,例如
最好还是使用不显眼的 JavaScript,例如(如果您使用的是 YUI):
It's better to have a single function to handle both events, e.g.
Better still use unobtrusive JavaScript, e.g. (if you were using YUI):