Javascript (JsTree):如何引用树节点的子元素

发布于 2024-12-03 03:40:21 字数 933 浏览 10 评论 0原文

您好,我正在开发一个 Symfony 项目,我需要 JsTree 结构来表示数据库中的层次结构。我声明

$("#myTree").bind("select_node.jstree",function(event,data){
    if(data.rslt.obj.attr("id")!=0){
        window.location = "myModule/myAction?id="+data.rslt.obj.attr("id");
    }
});

问题是链接无法按预期工作。当我单击时,window.location 在路线末尾添加“myModule/myAction?id=”+“所选 id”。

当我尝试更复杂的方法来指示要前往的新路线时,例如 window.location = +"/id/"+data.rslt .obj.attr("id"); 那么树的表示失败:它不像树那样显示,只是作为列表显示。

我认为如果有某种方法可以做到这样的事情是可能的: window.location = data.rslt.obj.attr("id").subelement("a").attr("href"); 然后我就可以访问中包含的信息>

  • 字段。
  • 非常感谢您抽出时间。

    Hi i'm working on a Symfony project and i need the JsTree structure to represent hierarchies from a database. I declare the <ul> & <li> structure espected by the JsTree libray correctly. Each <li> has its id = the id of the corresponding element in database, I put an <a> with its href=[the url i need to use when click]. Then i use the following function:

    $("#myTree").bind("select_node.jstree",function(event,data){
        if(data.rslt.obj.attr("id")!=0){
            window.location = "myModule/myAction?id="+data.rslt.obj.attr("id");
        }
    });
    

    The problem is that the link does not work as espected. When i click, window.location adds "myModule/myAction?id="+"the selected id" at the end of the route.

    When i try more complex ways to indicate the new route to go to, for example window.location = <?php echo url_for("myModule/myAction")?>+"/id/"+data.rslt.obj.attr("id"); then the representation of the tree fails: it does not show like a tree, just as a list.

    I think it would be possible if there is some way to do something like that:
    window.location = data.rslt.obj.attr("id").subelement("a").attr("href"); then i could access to the information contained in the <li> field.

    Thank you very much for your time.

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(2

    独闯女儿国 2024-12-10 03:40:21

    如果你能找到<的id李> 获取它的值

    然后你可以通过var id = data.rslt.obj.attr("id");

    $("#"+id).val();

    if you can fin the id of the < li> then you can obtains it's value by

    var id = data.rslt.obj.attr("id");

    $("#"+id).val();

    七分※倦醒 2024-12-10 03:40:21

    我终于找到了答案。有一个名为 childNodes 的函数,它返回对象的所有子元素。我使用了下面的代码:

    var id = data.rslt.obj.attr("id");

    var child = document.getElementById(id).childNodes[1].firstChild;< /code>

    请参阅文档:childNodes 文档

    I finaly found the answer. There is a function called childNodes that returns all the subelements of an object. I used the code below:

    var id = data.rslt.obj.attr("id");

    var child = document.getElementById(id).childNodes[1].firstChild;

    see documentation: childNodes doc

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文