将 javascript 属性事件转发到不同的处理程序/类型

发布于 2024-08-05 02:33:26 字数 408 浏览 2 评论 0原文

我目前正在尝试为触摸屏计算机设置一个网页,并且注意到(用我的胖手指)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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

顾铮苏瑾 2024-08-12 02:33:26

好的,编辑之后就变得更清楚了 - 您想要做的是在触发 mousedown 事件时模拟用户单击控件。

两个选项:

  1. 您可以触发 DOM 事件 - 本关于 DOM 事件的教程讨论了this(请参阅手动触发事件部分); 触发 Javascript 事件 涵盖了类似的内容。
  2. 设置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:

  1. You can fire the DOM event - this tutorial on DOM events discusses this (see the Manually firing events section); Firing Javascript Events covers similar ground.
  2. Set onmouseout="this.checked = true;", i.e. clicking the radio button "checks"/selects the radio button.
脸赞 2024-08-12 02:33:26

为什么不直接处理 mousedown 事件呢?

Why not just handle the mousedown event?

遇到 2024-08-12 02:33:26

我知道你在问题中没有提到它,但如果你碰巧使用 jQuery,那就真的很简单:

$().live('mousedown', function() { this.click(); });

I know you didn't mention it in the question, but if you happen to be using jQuery, it'd be really really easy:

$().live('mousedown', function() { this.click(); });
野の 2024-08-12 02:33:26

也许这对您有用

document.getElementById("yourinput").onmousedown = function() {
   this.click();
}

此外,您可以将输入按钮/字段的样式设置得更大一些,以便通过指针更好地访问。

Maybe this works for you

document.getElementById("yourinput").onmousedown = function() {
   this.click();
}

Additionally you could style your input buttons/fields to be a little bigger, for better accessibility via the pointer.

像极了他 2024-08-12 02:33:26

最好有一个函数来处理这两个事件,例如

var function = foo(e) {
   ...
};

<input onclick="foo();" onmousedown="foo();"/>

最好还是使用不显眼的 JavaScript,例如(如果您使用的是 YUI):

<input id="input-el"/>

var function = foo(e) {
   ...
};

var Event = YAHOO.util.Event;

Event.on('input-el', 'click', foo);
Event.on('input-el', 'mousedown', foo);

It's better to have a single function to handle both events, e.g.

var function = foo(e) {
   ...
};

<input onclick="foo();" onmousedown="foo();"/>

Better still use unobtrusive JavaScript, e.g. (if you were using YUI):

<input id="input-el"/>

var function = foo(e) {
   ...
};

var Event = YAHOO.util.Event;

Event.on('input-el', 'click', foo);
Event.on('input-el', 'mousedown', foo);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文