javascript 如何取消选择jquery中的所有元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23730499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to deselect all element in jquery?
提问by Manwal
I want to deselect (remove selected element ).I am using jstree in my demo .So I read the plugin api . http://www.jstree.com/api/#/?f=deselect_all([supress_event]). But it not deleselect the selected item. I follow the these steps 1) Click the "b" node.then "b" node is selected. 2) Then press "deselect" button but it not deselect the items.
我想取消选择(删除选定的元素)。我在演示中使用了 jstree。所以我阅读了插件 api。 http://www.jstree.com/api/#/?f=deselect_all([supress_event])。但它不会取消选择所选项目。我按照以下步骤操作 1) 单击“b”节点。然后选择“b”节点。2)然后按“取消选择”按钮,但不会取消选择项目。
$('#deselect').click(function(){
alert('--')
$('#tree').deselect_all(true)
})
回答by Manwal
Try this code:
试试这个代码:
$('#tree').jstree("deselect_all");
Insted of
插入的
$('#tree').deselect_all(true)
Here is Updated fiddle
回答by EspressoBeans
I just learned that you can also use:
我刚刚了解到您还可以使用:
$('#tree').jstree("deselect_all", true);
$('#tree').jstree("deselect_all", true);
This will suppress the 'changed.jstree' event, which is what I needed for my page, and seems to be more along the lines of your original logic.
这将抑制 'changed.jstree' 事件,这是我的页面所需要的,并且似乎更符合您的原始逻辑。
My working code excerpt verbatim:
我的工作代码逐字摘录:
function DeselectTree() {
$('[id ^= "forms-foldertree-"]').each(function () {
$(this).jstree('deselect_all', true);
});
}
回答by Ajay2707
What I use,
我用的,
$('#deselect').click(function(){
$('#tree').attr("checked", false);
})
$('#deselect').click(function(){
$('#tree').attr("checked", false);
})
or you can add custom attributes and give stylesheet/class for select/deselect
或者您可以添加自定义属性并为选择/取消选择提供样式表/类
by jquery change this
通过 jquery 改变这个
$('#deselect').click(function(){
if($('#tree').attr("customattr") == "select" )
$('#tree').attr("customattr", "unselect");
else
$('#tree').attr("customattr", "select");
})
$('#deselect').click(function(){
if($('#tree').attr("customattr") == "select" )
$('#tree').attr("customattr", "unselect");
别的
$('#tree').attr("customattr", "select");
})