javascript - 使用 jQuery 取消选择选定区域

标签 javascript jquery html selectable

我有以下 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>

以及以下 jQuery:

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

当我想取消选择所选项目时,一切都很好接受。无论我点击哪里,都不会发生任何事情。我没有找到任何可以帮助的东西。

最佳答案

如果我理解正确的话,没有直接的方法可以做到这一点,但你可以使用一个技巧。

当用户单击容器外部的某个位置(.folder)时,您应该做两件事:

  1. 删除所选项目的 .ui-selected 类。
  2. 清除小部件实例的选定列表。

注意:这是一个通用解决方案,因此可能会与您的其他代码(例如 $('html').click)发生冲突,因此请小心。

代码:

$( "#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>

关于javascript - 使用 jQuery 取消选择选定区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35777982/

相关文章:

javascript - 将 javascript 数组值分配给 angularjs 变量

javascript - 将值传递给 jQuery 中的函数

javascript - 在 jqGrid 中调用所选行仅有效一次

javascript - Material Ui 代码在 codepen 中运行良好,但在 VSCODE 中运行不佳?

javascript - 使用 javascript 添加换行符

javascript - 使用 Requirejs 的非 AMD 库

javascript - 使用 jquery 显示数据库结果的 php 循环中的特定文本

html - CSS轮廓宽度不起作用

javascript - 如何获取 super 父div宽度并根据 super 父div宽度设置子和父div宽度

javascript - 如何用Jasmine编写参数化测试?