简体   繁体   English

如何从DOM元素中删除事件侦听器?

[英]How to remove event listener from DOM element?

I would like to toggle add/remove event listeners on click. 我想在点击时切换添加/删除事件监听器。 Could someone point out how I could improve my code? 有人可以指出我如何改进我的代码?

I've read a number of answers on Stackoverflow but none have helped me. 我在Stackoverflow上阅读了很多答案,但没有人帮助过我。

This is a learning tool where hovering the pointer over the displayed numbers makes the center cube rotate to display the same number. 这是一种学习工具,将指针悬停在显示的数字上会使中心立方体旋转以显示相同的数字。

When the page loads an event listener is added to the centre cube and I wrote a little jQuery to add listeners to any clicked cube so they will also show the number that the pointer hovers on. 当页面加载时,事件监听器被添加到中心多维数据集中,我写了一个小jQuery来向任何单击的多维数据集添加监听器,这样它们也将显示指针悬停的数字。

Works in Chrome, sorta in Opera. 适用于Chrome,适用于Opera。 FF thinks its Salvador Dali and IE... duh! FF认为它的萨尔瓦多·达利和IE ......呃!

How it works at the moment 它目前是如何运作的

Event listener added on page load 事件监听器在页面加载时添加

var thisCube = '.two';
var init = function() {
  var box = document.querySelector(thisCube).children[0],
      showPanelButtons = document.querySelectorAll('#hover-change li'),
      panelClassName = 'show-front',

      hover = function( event ){
        box.removeClassName( panelClassName );
        panelClassName = event.target.className;
        box.addClassName( panelClassName );
      };

  for (var i=0, len = showPanelButtons.length; i < len; i++) {
    showPanelButtons[i].addEventListener( 'mouseover', hover, false);
  }

};

window.addEventListener( 'DOMContentLoaded', init, false);

jQuery to add new listener jQuery添加新的监听器

$('.one, .three, .four, .five, .six, .seven, .eight, .nine').click(function() { 
    thisCube = $(this).data('id');
    init();
});

I'm sure I'm not adding the event listeners correctly and so that is causing my trouble when I read about other solutions. 我确定我没有正确添加事件监听器,因此当我阅读其他解决方案时,这会给我带来麻烦。

I didn't build a jsfiddle for this post as the new rule of posting all relevant code would make this post far too large. 我没有为这篇文章建立一个jsfiddle,因为发布所有相关代码的新规则会使这篇文章过于庞大。 If anyone would like to see it on jsfiddle please ask. 如果有人想在jsfiddle上看到它请问。

Built on this demo 以此演示为基础

--EDIT-- - 编辑 -

I tried adding this code to add a class rotate and if an element has that class already I remove rotate and remove the event listener. 我尝试添加此代码以添加类rotate ,如果元素已经具有该类,我将删除rotate并删除事件侦听器。 It wont remove the event listener. 它不会删除事件监听器。

$('.one, .two, .three, .four, .five, .six, .seven, .eight, .nine').on('click', function() {

    if($(this).hasClass('rotate'))
    {
        $(this).removeClass('rotate');
        alert('remove ' + $(this).attr("class"));
        $(this).off('click');
    } else {
        $(this).addClass('rotate');
        alert('add ' + $(this).attr("class")); 
        thisCube = $(this).data('id');
        init();
    }
});

-- My Solution -- - 我的解决方案 -

$('.container').click(function(){
    $(this).toggleClass('rotate');
});

$('#hover-change').children().hover(function(){
    $('.rotate > #cube').removeClass();
    $('.rotate > #cube').addClass($(this).attr('class'));
},
function(){});

You can use jQuery.on to add event listeners and jQuery.off to remove them. 您可以使用jQuery.on添加事件侦听器,使用jQuery.off删除它们。

//Add
$('.one, .three, .four, .five, .six, .seven, .eight, .nine').on('click', function() { 
    thisCube = $(this).data('id');
    init();
});

// Remove
$('.one, .three, .four, .five, .six, .seven, .eight, .nine').off('click');

You can use Event Namespace as well: 您也可以使用Event Namespace:

//Add
    $('.one, .three, .four, .five, .six, .seven, .eight, .nine').on('click.MyNamespace', function() { 
        thisCube = $(this).data('id');
        init();
    });

// Remove
    $('.one, .three, .four, .five, .six, .seven, .eight, .nine').off('click.MyNamespace');

This way you don't mess with other handlers.. 这样你就不会乱搞其他处理程序..

Hope it helps... 希望能帮助到你...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM