触摸事件不适用于 iPad Safari 中的文本输入元素吗?

发布于 2024-09-11 01:38:33 字数 634 浏览 13 评论 0原文

在 iPad Safari 中,我对 DIV 的触摸事件进行了编程,以便当您触摸 DIV 内部并将手指按住 500 毫秒时,DIV 的背景颜色会发生变化。

当我尝试将代码移至文本输入元素(字段集中),使其成为触摸目标而不是 DIV 时,代码不起作用。尽管有此 CSS,文本输入仍会被选中:

    input[type=text] {-webkit-touch-callout:none; -webkit-user-select:none }

是否没有办法拦截 iPad Safari 中文本输入元素的触摸事件并以自定义方式处理它们,从而防止默认行为?或者我还必须做些什么才能获得支持这一点的输入?我尝试过使用和不使用虚拟点击处理程序:onclick =“void(0)”。

这是我正在关注的文档 处理事件

In iPad Safari, I have programmed a DIV's touch-events so that when you touch inside the DIV and hold your finger there for 500 ms, the DIV's background color changes.

When I try to move the code over to a text-input element (inside a fieldset), making it the touch-target instead of the DIV, the code doesn't work. The text-input becomes selected in spite of this CSS:

    input[type=text] {-webkit-touch-callout:none; -webkit-user-select:none }

Is there no way to intercept the touch events of a text-input element in iPad Safari and handle them in a custom manner, preventing default behavior? Or is there something additional that I must do to get the input to support this? I've tried with and without a dummy click handler: onclick="void(0)".

This is the doc I'm following the documentation Handling Events.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

维持三分热 2024-09-18 01:38:44

尝试禁用并使您的输入只读:

<input type="text" readonly />

Try to disable and make your input readonly:

<input type="text" readonly />
银河中√捞星星 2024-09-18 01:38:42

如果您不在处理程序代码中调用 preventDefault(),浏览器会自动将触摸事件传递给默认实现(在您认为已经处理完之后)。默认实现是选择字段。

因此,在您的情况下,请在处理程序中调用 preventDefault()stopPropagation() ,并返回 false。这可以防止事件进一步冒泡。然后你就可以完全控制你的输入元素。

警告:然后您还将失去输入字段的默认行为!换句话说,您将无法在该字段中输入文本!或者,如果输入字段是

我想您真正想要的是:1)如果用户按住 500 毫秒,然后变为黄色,2) 和/或释放时激活输入字段。在这种情况下,当您确实想要使用输入字段时,您必须手动向上重新触发事件。

这种情况在iPad编程时很常见。例如,在许多情况下,您希望检测触摸拖动动作(即使在输入字段上)并将其解释为移动,但将触摸释放动作(不移动)解释为单击以激活输入字段。您必须执行我上面建议的操作:preventDefault() 并在必要时重新触发事件。

If you don't call preventDefault() in your handler code, the browser will automatically pass the touch event through to the default implementation (after you think you've handled it). The default implementation is to select the field.

So, in your case, call preventDefault() and stopPropagation() in your handler, and return false. This prevents the event from bubbling further. Then you can totally control your input element.

Caveat: You'll then also lose the default behavior of the input field! In other words, you'll not be able to input text into the field! Or if the input field is a <select>, you won't be able to pull up the list etc.

I suppose what you really want is: 1) If user presses and hold for 500ms, then turn yellow, 2) and/or on release activate the input field. In that case, you'll have to manually refire the event upwards when you really want to use the input field.

This kind of situation is very common when programming the iPad. For example, in many cases you'd want to detect a touch-drag motion (even on an input field) and interpret it as a move, but interpret a touch-release motion (without moving) as a click to activate the input field. You have to do what I suggest above: preventDefault() and refire event when necessary.

十年九夏 2024-09-18 01:38:40

如果您发布代码将会很有帮助,但我认为您可能只需要防止触摸事件的默认行为。如果使用 jQuery,这看起来像这样:

$("#ID")
        .bind("touchstart",
            function (e) {
                e.preventDefault();
                //do something
            })
        .bind("touchmove",
            function (e) {
                if (e.preventDefault) { 
                    e.preventDefault();
                }
                //do something
            });

It would be helpful if you posted your code, but I'm thinking you probably just need to prevent the default behavior on touch events. This looks something like this if using jQuery:

$("#ID")
        .bind("touchstart",
            function (e) {
                e.preventDefault();
                //do something
            })
        .bind("touchmove",
            function (e) {
                if (e.preventDefault) { 
                    e.preventDefault();
                }
                //do something
            });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文