简体   繁体   English

用jQuery取消选择选定区域

[英]Deselect a selected area with jQuery

I have the following HTML: 我有以下HTML:

<div class="folders block">
    <div class="ui-widget-content">            
         <table class="folders-list">
             <tbody class="folder">
                  <tr><td style="text-align: center">Папки: </td></tr>
                  <tr class="folder-name"><td><i class="fa fa-folder-open"></i><span>Всички</span></td></tr>          
             </tbody>
         </table>
    </div>
</div>

And the following jQuery: 和下面的jQuery:

$(".folder").selectable({
    stop: function() {
        $(".ui-selected", this)
            .each(function() {
                var index = $("table tbody").index(this);   
            });     
    }
});

Everything is fine accept when I want to deselect the selected item. 当我想取消选择所选项目时,一切都可以接受。 Wherever I click nothing happens. 我单击任何地方都不会发生任何事情。 I didn't find anything that could help. 我没有找到任何可以帮助的东西。

If I understand you correctly, there is no straightforward way to do this, but you can use a trick. 如果我对您的理解正确,则没有简单的方法可以执行此操作,但是可以使用技巧。

When the user click on somewhere outside the container ( .folder ) you should do 2 things: 当用户单击容器外部的某个位置( .folder )时,您应该做两件事:

  1. Remove the .ui-selected class of the selected items. 删除所选项目的.ui-selected类。
  2. Clear the selected list of the widget's instance. 清除小部件实例的选定列表。

Note: It's a generic solution so maybe it will conflict with your other code ( $('html').click for example) so be careful. 注意:这是一个通用解决方案,因此它可能会与您的其他代码(例如$('html').click冲突,因此请小心。

To the code: 到代码:

 $( "#selectable" ).selectable().click(function(event) { event.stopPropagation(); }); $('html').click(function(){ var ins = $( "#selectable" ).selectable( "instance" ); // clear the selected list ins.selectees = []; // remove the selected class ins.element.find('.ui-selected').removeClass('ui-selected'); }); 
 #feedback { font-size: 1.4em; } #selectable .ui-selecting { background: #FECA40; } #selectable .ui-selected { background: #F39814; color: white; } #selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; } #selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; } 
 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <ol id="selectable"> <li class="ui-widget-content">Item 1</li> <li class="ui-widget-content">Item 2</li> <li class="ui-widget-content">Item 3</li> <li class="ui-widget-content">Item 4</li> <li class="ui-widget-content">Item 5</li> <li class="ui-widget-content">Item 6</li> <li class="ui-widget-content">Item 7</li> </ol> 

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

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