简体   繁体   English

D3从选择中的一个元素中删除点击事件

[英]D3 remove click events from one element out of a selection

I have a class in D3 say: selectors and I need to remove the click event from the selection 我在D3中有一个课说:选择器,我需要从选择中删除click事件

d3.selectAll('.selectors').on('click',function(){
       //Remove the currently clicked element from the selection.
});

Ive got two problems: 我有两个问题:

  1. The removed element is supposed to be moved to a different part of the page and the I need the click event on it to be removed. 应该将已删除的元素移动到页面的其他部分,并且我需要将其上的click事件删除。

  2. Also, would it be possible to reinsert the removed element into the selection on doing something else, like clicking on the removed element again? 另外,是否可以在执行其他操作(例如再次单击删除的元素)时将删除的元素重新插入选择中?

Edit: 编辑:

Found a solution for problem 1 找到问题1的解决方案

d3.selectAll('.selectors').on('click',function(){
           //Remove the currently clicked element from the selection.
           d3.select(this).on('click',null);
    });

Is this the right way? 这是正确的方法吗? Or is there a more graceful method? 还是有一个更优雅的方法?

A Demo Fiddle 演示小提琴

here is the updated jquery it will work for your case 这是更新的jQuery,它将适合您的情况

$(document).ready(function(){
$(document).on('click','.selectors',function(e){
 //$(document).off( 'click','.selectors');
  if(e.target.onclick==null)
  {
      e.target.onclick=
          function(){
          void(0);
      };
     alert('test');
     console.log('Hello');
  }
});
});

For problem 1, the best method(as far as I know) is to redefine the click event in D3 itself: 对于问题1,最好的方法(据我所知)是在D3本身中重新定义click事件:

d3.selectAll('.selectors').on('click',function(){
           //Remove the currently clicked element from the selection.
           d3.select(this).on('click',null);
    });

For problem 2, however, once you turn a click event callback to null, the only way is to redefine the click event again, perhaps recursively: 但是,对于问题2,一旦将click event回调设为null,唯一的方法是重新定义click事件,也许是递归地:

function clickDefine() {
    d3.selectAll('.selectors').on('click', function () {
        //Remove the currently clicked element from the selection.
        console.log('Hello')
        d3.select(this).on('click', null);
        setTimeout(function(){clickDefine();},1000)
    });
}

This function makes the click event inactive for 1 second on click. 此功能使点击事件在点击后1秒钟内处于非活动状态。 And reactivates this again. 并再次激活它。 I'm hoping this is an effective solution. 我希望这是一个有效的解决方案。

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

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