我是JQuery/Jstree的新手。当我试图以编程方式选择一个节点时,它总是返回容器节点,而不是我想要得到的子节点。请帮帮忙。
我的代码如下,
<div id="containerId" class="demo">
<ul>
<li id="phtml_1">
<a href="#">Root node 1</a>
<ul>
<li id="zzx">
<a href="#">Child node 1</a>
</li>
<li id="phtml_3">
<a href="#">Child node 2</a>
</li>
</ul>
</li>
</ul>
javascript
$(function () {
$("#containerId").jstree({
"plugins" : [ "themes", "html_data", "ui" ]
});
});脚本
$(document).ready(function () {
var node= $("#containerId").jstree("select_node","#zzx");
//Problem here, why the id is still the "containerId", not the "zzx"?
alert(node.attr("id"));
}); -谢谢--罗杰
发布于 2011-07-17 04:54:33
您将node设置为等于#('containerId'),而不是其他任何东西。在jQuery中调用对象上的方法时,结果只是对象。这样,您就可以链接这样的方法:
$('#containerId').method1().method2().method3();如果要以编程方式选择节点,则已经正确地这样做了:
$("#containerId").jstree("select_node","#zzx");由于您知道您刚刚选择的节点,所以可以像使用jQuery的任何其他元素一样获得节点:
$('#zzx')如果每次选择节点时都尝试调用函数,请使用以下命令:
$('#containerId').bind('select_node.jstree', function (event, data) {
alert(data.rslt.obj.attr('id'));
})https://stackoverflow.com/questions/5898919
复制相似问题