Javascript (JsTree):如何引用树节点的子元素
您好,我正在开发一个 Symfony 项目,我需要 JsTree 结构来表示数据库中的层次结构。我声明
结构。每个
- 都有其 id = 数据库中相应元素的 id,我放置了一个
及其
href=[我的 url点击时需要使用]
。然后我使用以下函数:
; & 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你能找到<的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();
我终于找到了答案。有一个名为
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