我正在以这种方式在项目中使用jqTree:
最后一步是我遇到麻烦的地方。这是我正在使用的代码:
$('#compare-button').click(function(event){
event.preventDefault();
var last = false;
$('#output p.root').each(function(){
if ($(this).is(':last-child'))
{ last = true; } // set last = true on the last p.root
$(this).tree({
dataUrl: 'http://path/to/service/',
autoOpen: true,
dragAndDrop: false,
onCreateLi: function(node, $li){
$li.attr("value", node.id);
if (total_count.hasOwnProperty(node.id)) { total_count[node.id]++; }
else { total_count[node.id] = 1; }
if (last && $li.is(':last-child')) // Once we're done with our calls to .tree(), fire the highlighting code.
{
$.each(total_count, function(key, value){
if (value > 1) { $('li[value="' + key +'"] span').addClass('highlight'); }
});
}
} // end of onCreateLi
}) // end of tree()
}); // end of each()
}); // end of click()
问题是'onCreateLi'在每个li之后触发,因此它刚创建的那个总是是最后一个兄弟。我还在.each()的末尾尝试了一个.load(),但它似乎打断了树的构建(我猜它在构建树之前就被触发了)。
如果jqTree有.afterLoad()事件处理程序,这会容易得多。
编辑:等等,这实际上可能正常......
编辑2:在Firefox中运行,而不是在IE中运行。
编辑3:我将以不同的方式提出这个问题,因此更多不熟悉jqTree的jQuery专家可以回答这个问题。我正在使用jQuery.each()循环遍历每个'p.root'元素。完成循环后,我需要激活一些代码。我尝试了jQuery.each()。after(),但它似乎打断了树的构建。