简体   繁体   English

防止用户在div褪色时单击div

[英]Preventing the user to click a div when its fading out

I'm trying to create a filtering system where the user can filter for answers using more than one category, but the user can't choose more than one option from each category, so upon clicking an option from any category, that category will fade out ... I have the following little problem, if the user clicks on another option from the same category while it's fading out, he will be able to choose two options from the same category ... How can I disable any click on the category when it's fading out ?? 我正在尝试创建一个过滤系统,用户可以使用多个类别来过滤答案,但用户不能从每个类别中选择多个选项,因此在单击任何类别的选项时,该类别将消失我有以下小问题,如果用户在淡出时单击同一类别中的另一个选项,他将能够从同一类别中选择两个选项...如何禁用对类别何时消失?

Here is the code I'm using: 这是我正在使用的代码:

HTML: HTML:

<div class="search-list" id="program-list">
   <span class="search-title">Category 1</span>
   <a href="javascript:void(0)" class="search-option"><span data="program-list">Option 1</span></a>
   <a href="javascript:void(0)" class="search-option"><span data="program-list">Option 2</span></a>
   <a href="javascript:void(0)" class="search-option"><span data="program-list">Option 3</span></a>
   <a href="javascript:void(0)" class="search-option"><span data="program-list">Option 4</span></a>
   <a href="javascript:void(0)" class="search-option"><span data="program-list">Option 5</span></a>
   <a href="javascript:void(0)" class="search-option"><span data="program-list">Option 6</span></a>
</div> <!-- search-list -->

<div class="search-list" id="trainer-list">
    <span class="search-title">Category 2</span>
    <a href="javascript:void(0)" class="search-option"><span data="trainer-list">Option 1</span></a>
    <a href="javascript:void(0)" class="search-option"><span data="trainer-list">Option 2</span></a>
    <a href="javascript:void(0)" class="search-option"><span data="trainer-list">Option 3</span></a>
    <a href="javascript:void(0)" class="search-option"><span data="trainer-list">Option 4</span></a>
 </div> <!-- search-list -->

 <div class="search-list" id="issue-date">
     <span class="search-title">Category 3</span>
     <a href="javascript:void(0)" class="search-option"><span data="issue-date">Option  1</span></a>
     <a href="javascript:void(0)" class="search-option"><span data="issue-date">Option 2</span></a>
     <a href="javascript:void(0)" class="search-option"><span data="issue-date">Option 3</span></a>
     <a href="javascript:void(0)" class="search-option"><span data="issue-date">Option 4</span></a>
     <a href="javascript:void(0)" class="search-option"><span data="issue-date">Option 5</span></a>
     <a href="javascript:void(0)" class="search-option"><span data="issue-date">Option 6</span></a>
 </div> <!-- search-list -->

JQuery: jQuery的:

   $('.search-option').click(function(){
       var x = $(this).html();
       $('#filtered-items').append(x);
       $(this).parent('.search-list').fadeTo('slow', 0);

       $('#filtered-items > span').click(function(){
          $('#'+$(this).attr('data')).fadeTo('slow', 100);
          $(this).hide();
           });
        });

I tried using the following JQuery code but it didn't work: 我尝试使用以下JQuery代码,但没有成功:

 $('.search-option').click(function(e) {
            e.preventDefault();
       });  

Thanks in advance... 提前致谢...

You can set an indicator in your function: 您可以在函数中设置指标:

$('.search-option').click(function(){
   if(indicator) {
     return false
   }
...
}

The indicator is something set when the fade starts and clears when it ends. 淡入淡出开始时设置指示器,结束淡入时清除。 It could be: 它可能是:

  • a javascript variable, 一个javascript变量,

  • or - you can check the current opacity, 或-您可以检查当前的不透明度,

  • or - or you can set/clear a class name during the fade. 或-或您可以在淡入淡出期间设置/清除班级名称。

Try: 尝试:

$('.search-option').click(function(){
  var x = $(this).html();
  $('#filtered-items').append(x);

  // ADDED LINES  
  $(this).parent('.search-list').find('a').each(function(){
    $(this).click(function(){return false;});
  });
  // END: ADDED LINES   

  $(this).parent('.search-list').fadeTo('slow', 0);

  $('#filtered-items > span').click(function(){
    $('#'+$(this).attr('data')).fadeTo('slow', 100);
    $(this).hide();
  });
});

只需单击类别中的所有选项中的“搜索选项”类即可。

You can set the click event to null once it has fired: 您可以将click事件触发后将其设置为null:

$('.search-option').click(function(){
   $(this).click(null); // set click to do nothing on the clicked element
   var x = $(this).html();
   $('#filtered-items').append(x);
   $(this).parent('.search-list').fadeTo('slow', 0);

   $('#filtered-items > span').click(function(){
      $('#'+$(this).attr('data')).fadeTo('slow', 100);
      $(this).hide();
   });
});

This will only turn off the click action on the specifically clicked element so your other filters will continue to work until each of them has been selected once. 这只会关闭特定点击元素上的点击动作,因此您的其他过滤器将继续起作用,直到每个过滤器都被选中一次为止。 If you want to re-enable the click function after everything has happened you can save the function, disable it, then re-enable at the end as below: 如果您想在所有事情发生后重新启用click功能,则可以保存该功能,将其禁用,然后在末尾重新启用,如下所示:

$('.search-option').click(function(){
   var fun = $(this).click;
   $(this).click(null); // set click to do nothing on the clicked element

   // OTHER CODE

   $(this).click(fun);
});

Since this all hinges on the user clicking on an element with the class "search-option", just remove that class from the element and its siblings before starting the fade and then restore it with a callback after the fade runs. 由于所有这些都取决于用户单击具有“搜索选项”类的元素,因此只需在开始渐变之前从该元素及其同级中移除该类,然后在渐变运行后通过回调将其还原。 That way, nobody can click twice: 这样,没有人可以单击两次:

   $('.search-option').click(function() {
        var x = $(this).html();
        $('#filtered-items').append(x);
        $(this).parent().children().removeClass('search-option');
        $(this).parent('.search-list').fadeTo('slow', 0, function() {
             $(this).children().addClass('search-option');
        });
   });

Hey everyone ... I finally solved it, I had to change the whole structure of the function and use the methods "unbind" and "bind" ... Here is the solution if anyone is interested in it: 大家好...我终于解决了它,我不得不更改函数的整体结构,并使用“ unbind”和“ bind”方法...如果有人对此感兴趣,可以使用以下解决方案:

        $('.search-option').click(work);

    });

 function work(){


                var x = $(this).html();     

                $('#filtered-items').append(x);

                $(this).unbind('click');
                $(this).siblings('a').unbind('click');

                $(this).parent('.search-list').fadeOut('slow');

        $('#filtered-items > span').click(function(){
                $('#'+$(this).attr('data')).fadeIn('slow');
                $('#'+$(this).attr('data')).children('a').bind('click',work );
                $(this).remove();
                });

            };                  

Now the user can't choose more than one option at each category .... 现在,用户不能在每个类别中选择多个选项。

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

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