我有以下 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
)时,您应该做两件事:
- 删除所选项目的
.ui-selected
类。 - 清除小部件实例的选定列表。
注意:这是一个通用解决方案,因此可能会与您的其他代码(例如 $('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/