最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML with event.preventDefault - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Aug 18, 2010 at 2:30 YazzmiYazzmi 1,5715 gold badges18 silver badges33 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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.

发布评论

评论列表(0)

  1. 暂无评论