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

javascript - Opposite of e.preventDefault and e.stopPropagation(); - Stack Overflow

programmeradmin1浏览0评论

I wrote some javascript code that prevents scrolling on focus. How do I disable it after focus is lost?

Focus

e.preventDefault();
e.stopPropagation();

Focus out

e.preventDefault = false;
e.stopPropagatio = false;

I wrote some javascript code that prevents scrolling on focus. How do I disable it after focus is lost?

Focus

e.preventDefault();
e.stopPropagation();

Focus out

e.preventDefault = false;
e.stopPropagatio = false;
Share Improve this question asked Jan 20, 2017 at 13:42 user5804438user5804438 3
  • 1 Would it be worth trying to unbind the event listener? Or put the methods behind a boolean flag? – evolutionxbox Commented Jan 20, 2017 at 13:43
  • 1 You can't undo them. Just stop calling them. – Oriol Commented Jan 20, 2017 at 13:46
  • @evolutionxbox that fixed it :). Thanks! – user5804438 Commented Jan 20, 2017 at 13:52
Add a ment  | 

2 Answers 2

Reset to default 4

Fixed by unbinding the event.

Focus

    $('.form-control').focus(function() {  
            $('#element').on('scroll touchmove mousewheel', function(e){
                e.preventDefault();
                e.stopPropagation();
        })
});

Focus out

$('.form-control').focusout(function() {
        $('#element').unbind();
});

You can't undo preventDefault nor stopPropagation.

Just stop calling them, e.g.

var stopScroll = true;
element.addEventListener('scroll', function() {
  if (stopScroll) {
    e.preventDefault();
    e.stopPropagation();
  }
});
// ...
stopScroll = false;
function listener() {
  e.preventDefault();
  e.stopPropagation();
}
element.addEventListener('scroll', listener);
// ...
element.removeEventListener('scroll', listener);
发布评论

评论列表(0)

  1. 暂无评论