带有event.preventDefault的HTML [英] HTML with event.preventDefault

查看:100
本文介绍了带有event.preventDefault的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果html文档被禁用。 (不滚动...)

  document.ontouchmove = function(event){event.preventDefault(); } 

我可以选择性地在html文档中启用元素吗?如div,跨越
我应该用什么javascript来做到这一点?

解决方案

这是一个相当不好的解决方案。



更改您的 touchmove 处理程序,如下所示:

 document.ontouchmove = function(event){
if(!event.elementIsEnabled){
event.preventDefault();
}
};

然后对于您要启用的元素:

  element.ontouchmove = function(event){
event.elementIsEnabled = true;
};

这是因为:


  • 当元素触发 touchmove 事件时,这两个事件处理程序将在冒泡阶段处理该事件。这意味着元素 touchmove 处理程序首先触发,因为它最接近事件源。 当两个事件处理程序处理相同的事件,他们收到相同的事件对象。

  • 您可以在事件对象上设置自己的任意属性。



因此,元素 touchmove 处理程序只是在事件对象上设置一个自定义属性,然后文档 touchmove 处理程序检查该定制属性以确定是否应该阻止默认操作。


If the html document is disabled. (no scrolling...)

document.ontouchmove = function(event){ event.preventDefault(); }

Can I selectively enabled elements in the html document? such as a div, span What javascript should I use to do that?

解决方案

Here’s a fairly hacky solution.

Change your touchmove handler as follows:

document.ontouchmove = function (event) {
    if (!event.elementIsEnabled) {
        event.preventDefault();
    }
};

Then for an element that you want to enable:

element.ontouchmove = function (event) {
    event.elementIsEnabled = true;
};

This works because:

  • When a touchmove event is fired on the element, both of these event handlers handle the event in the bubbling phase. This means that the element touchmove handler fires first, because it is closest to the source of the event.
  • When two event handlers handle the same event, they receive the same event object.
  • You’re allowed to set your own arbitrary properties on an event object.

So, the element touchmove handler just sets a custom property on the event object, and then the document touchmove handler checks for that custom property to determine whether the default action should be prevented.

这篇关于带有event.preventDefault的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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