javascript 使用 jsTree 以编程方式检查复选框节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10053827/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:34:10  来源:igfitidea点击:

Checking a checkbox node programmatically with jsTree

javascriptjqueryjstree

提问by Raj

In a tree built with jsTree, I have the text within the <a>tag sitting in a variable. I would like to checkthat node. How can I do so?

在用 jsTree 构建的树中,我将<a>标签内的文本放在一个变量中。我想检查那个节点。我该怎么做?

I am currently finding that node, using jQuery, and altering its class. However, this does not repair the parent node by making the parent undeterminedin its class. I tried doing $('.colors').jstree("checkbox_repair"), but that didn't seem to do anything.

我目前正在使用 jQuery 查找该节点并更改其类。但是,这不会通过使父节点在其类中不确定来修复父节点。我试着做$('.colors').jstree("checkbox_repair"),但这似乎没有做任何事情。

It would be great if someone could actually answer both those questions, since they are related to the same problem.

如果有人能够真正回答这两个问题,那就太好了,因为它们与同一个问题有关。

Here is a jsFiddle, illustrating the issue--> http://jsfiddle.net/thapar/5XAjU/

这是一个 jsFiddle,说明了这个问题--> http://jsfiddle.net/thapar/5XAjU/

回答by jeffery_the_wind

In js_tree there are .check_node ( node )and .uncheck_node ( node )functions, i think this is what you are asking for. Soe the documentation here: http://www.jstree.com/documentation/checkbox

在 js_tree 中有.check_node ( node ).uncheck_node ( node )函数,我认为这就是你所要求的。Soe 文档在这里:http: //www.jstree.com/documentation/checkbox

This is an excerpt from the documentation in the link above, "how to perform an operation":

这是上面链接中文档的摘录,“如何执行操作”:

/* METHOD ONE */
jQuery("some-selector-to-container-node-here")
    .jstree("operation_name" [, argument_1, argument_2, ...]);

/* METHOD TWO */
jQuery.jstree._reference(needle)
    /* NEEDLE can be a DOM node or selector for the container or a node within the container */
    .operation_name([ argument_1, argument_2, ...]);

So I think this syntax should work

所以我认为这种语法应该有效

$.jstree._reference(".colors").check_node('li#tree_3');

Also i am not sure you should be using a class to reference your tree. Probably use an ID to reference your tree, and then use this syntax:

此外,我不确定您是否应该使用类来引用您的树。可能使用 ID 来引用您的树,然后使用以下语法:

$.jstree._reference("#colors").check_node('li#tree_3');

//EDIT: Please keep in mind that the newest version of jsTree doesn't have a function called _reference anymore. It got renamed to reference (without the leading underscore). (Last checked 24/08/2015 15:45 by @mkli90) Link: https://www.jstree.com/api/#/?f=$.jstree.reference(needle)

//编辑:请记住,最新版本的 jsTree 不再有名为 _reference 的函数。它被重命名为引用(没有前导下划线)。(上次检查时间为 24/08/2015 15:45 by @mkli90)链接:https: //www.jstree.com/api/#/ ?f =$.jstree.reference(needle)

回答by Aloe

If you want to check jsTree nodes on load for example like this:

如果要在加载时检查 jsTree 节点,例如像这样:

$(document).ready(function()
{
  $.jstree._reference('#menu').check_node('#pih2');
});

it does not work. For me works following:

这是行不通的。对我来说,工作如下:

$(function () {
    $('#mainMenu1').bind('loaded.jstree', function(e, data){ //waiting for loading
          $.jstree._reference('#menu').check_node('#pih2');  //check node with id pih2
        $.jstree._reference('#menu').check_node('#pih6');  //check node with id pih6
  }); 
});

I use jsTree 1.0-rc3 and JQuery 1.7.1. Aloe

我使用 jsTree 1.0-rc3 和 JQuery 1.7.1。芦荟