从页面中删除所有 Javascript 事件并将其放回原处

发布于 2024-10-03 09:10:31 字数 184 浏览 4 评论 0原文

我想“暂停”页面的所有事件,这意味着删除所有事件集......并能够在之后将它们放回去。

即使第一部分=删除页面的所有事件,我也不知道该怎么做!你有想法吗?

我没有发现任何有用的东西!

编辑:

为什么?我想在已经加载的页面中执行操作。用户将与页面交互,我想控制交互...所以删除之前设置的所有交互。

I'd like to "pause" all the events of a page which means removes all the event set... and be able to put them back after.

Even the first part = removing all the events of a page, I don't see how to do so! Do you have ideas?

I don't find anything working for it!

Edit:

Why? I want to act in a page already loaded. The user will interact with the page and I want to control the interaction... so remove all the interactions previously set.

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

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

发布评论

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

评论(6

北方。的韩爷 2024-10-10 09:10:35

您可以使用一个全局布尔变量 paused 来代替分离所有事件处理程序,该变量将在所有事件处理程序中进行检查。

var paused = false;

function handle_some_click(e) {
    if (!paused) {
        // do something
    }
}

Instead of detaching all event handlers, you can have a global boolean variable paused which would be checked in all event handlers.

var paused = false;

function handle_some_click(e) {
    if (!paused) {
        // do something
    }
}
捶死心动 2024-10-10 09:10:35

我不知道你为什么要这样做...但是你可以将所有的 javascript 放入外部 .js 文件中(正如你应该已经做的那样。然后将一个 javascript 函数硬编码到你的页面中...它看起来像像这样:

<script type="text/javascript" id="toggle" src="/myExternalJS.js"></script>
<script  type="text/javascript">
var hidden = false;

function toggleScripts() {
     if(hidden) {
          document.getElementById('toggle').setAttribute('src' '/none.js');
    } else {
          document.getElementById('toggle').setAttribute('src' '/myExternalJS.js');
    }
}

我认为这会起作用......
}

I'm not sure why you would do this... but you could put all of your javascript into an external .js file (as you should already. Then have a single javascript function that's hardcoded into your page... it would look like this:

<script type="text/javascript" id="toggle" src="/myExternalJS.js"></script>
<script  type="text/javascript">
var hidden = false;

function toggleScripts() {
     if(hidden) {
          document.getElementById('toggle').setAttribute('src' '/none.js');
    } else {
          document.getElementById('toggle').setAttribute('src' '/myExternalJS.js');
    }
}

I think this would work...
}

瑕疵 2024-10-10 09:10:34

这是我当前使用的解决方案:它并不能完全解决问题!

我在捕获阶段停止事件:我在文档对象上放置了一个侦听器来侦听捕获阶段的所有事件并停止传播。

+:

  • 列表项
  • 部分解决了问题

-:

  • 不适用于 IE(无捕获阶段)
  • 如果文档附加了其他事件,则会在我的侦听器之前触发。

您有更好的解决方案吗?

PS:如果找到更好的解决方案,我会继续更新这篇文章

Here is the currently solution I use: it doesn't solve the problem totally!

I stop the event during the capture phase: I put a listener on the document object to listen to all events in the capture phase and stop propagation.

+:

  • List item
  • Solves partially the problem

-:

  • doesn't work for IE (no capture phase)
  • if there are others events attached to the document, there are fired before my listener.

Do you have a better solution?

P.S.: I'll continue to update this post if I find a better solution

还给你自由 2024-10-10 09:10:34

对于任何 AJAX 交互,您当然至少可以使用 ajax 队列 中止任何连接,并且对于任何当前动画,您可以使用 stop();

for any AJAX interactions you can certainly at least abort any connections using ajax queue and for any current animations, you can use stop();

夏夜暖风 2024-10-10 09:10:33

您可以使用 unbind() 删除所有事件

$.unbind();

You can use unbind() to remove all events

$.unbind();
木落 2024-10-10 09:10:33

您需要将所有事件侦听器存储在 eventMap 中。

EventMap = {

    listeners:[],
    paused:false,

    mapListener:function(eventDispacter, eventType, eventHandler) {
        listeners.push({eD:eventDispacter, eT:eventType, eH:eventHandler});

        if(!paused)
            eventDispacter.addEventListener(eventType, eventHandler);
    },

    unMapListener:function(eventDispacter, eventType, eventHandler) {
        for( var i=0; i<listeners.length; i++) {
            var listener = listeners[i];

            if(listener.eD == eventDispacter && listener.eT == eventType && listener.eH == eventHandler) {
                listeners.splice(listeners.indexOf(listener), 1);
                eventDispacter.removeEventListener(eventType, eventHandler);
            }               
        }
    },

    pauseEvents:function() {
        if(!paused) {}
            for( var i=0; i<listeners.length; i++) {
                var listener = listeners[i];
                listener.eD.removeEventHandler(listener.eT, listener.eH);
            }
        }
    },

    unPauseEvents:function() {
        if(paused) {}
            for( var i=0; i<listeners.length; i++) {
                var listener = listeners[i];
                listener.eD.addEventHandler(listener.eT, listener.eH);
            }
        }
    }
}

添加事件

<script>
    function handleClick( e ) {
        alert('Button Clicked');
    }

    EventMap.mapListener(document.getElementById('myButton'), 'click', handleClick);
</script>

暂停事件

EventMap.pauseEvents();

You'd need to store all the event listeners in a eventMap.

EventMap = {

    listeners:[],
    paused:false,

    mapListener:function(eventDispacter, eventType, eventHandler) {
        listeners.push({eD:eventDispacter, eT:eventType, eH:eventHandler});

        if(!paused)
            eventDispacter.addEventListener(eventType, eventHandler);
    },

    unMapListener:function(eventDispacter, eventType, eventHandler) {
        for( var i=0; i<listeners.length; i++) {
            var listener = listeners[i];

            if(listener.eD == eventDispacter && listener.eT == eventType && listener.eH == eventHandler) {
                listeners.splice(listeners.indexOf(listener), 1);
                eventDispacter.removeEventListener(eventType, eventHandler);
            }               
        }
    },

    pauseEvents:function() {
        if(!paused) {}
            for( var i=0; i<listeners.length; i++) {
                var listener = listeners[i];
                listener.eD.removeEventHandler(listener.eT, listener.eH);
            }
        }
    },

    unPauseEvents:function() {
        if(paused) {}
            for( var i=0; i<listeners.length; i++) {
                var listener = listeners[i];
                listener.eD.addEventHandler(listener.eT, listener.eH);
            }
        }
    }
}

Add events with

<script>
    function handleClick( e ) {
        alert('Button Clicked');
    }

    EventMap.mapListener(document.getElementById('myButton'), 'click', handleClick);
</script>

Pause Events with

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