点击jquery中的treeview文本,避免树视图崩溃

时间:2010-01-05 13:29:57

标签: jquery treeview

我需要在点击treeview链接/文本时避免jquery treeview的崩溃。

例如,如果我在树状视图中显示directories/subdirectories列表,我只需在点击+/- image而不是点击directories

时折叠树视图

2 个答案:

答案 0 :(得分:9)

您需要更新treeview javascript代码本身。对于Treeview 1.4,请注释掉以下行(66-68):

this.filter(":has(>ul):not(:has(>a))").find(">span").click(function(event) {
    toggler.apply($(this).next());
}).add( $("a", this) ).hoverClass();

这将确保仅在+/-点击时发生展开/折叠。如果适用,展开全部和折叠所有功能也将继续有效。

更好的是,在定义树时提供新参数,并且只有在条件满足时才覆盖默认功能。例如,

if (settings.expandMode != 'simple'){
    this.filter(":has(>ul):not(:has(>a))").find(">span").click(function(event) {
        toggler.apply($(this).next());
    }).add( $("a", this) ).hoverClass();
}

您的树视图定义可能如下所示:

$("#tree").treeview({
    animated: "fast",
    persist: "cookie",
    collapsed: true,
    control: "#treecontrol",
    expandMode: "simple" // custom - only toggles per the +/- icon      
})

希望你明白了。祝你好运。

答案 1 :(得分:0)

将您的点击事件绑定到+/-图像,而不是包含图像和目录文本的容器。

因此,如果你的HTML看起来像这样:

<ul class="dir-list">
    <li>
        <div><img class="expand" src="expand.gif" /><span>Directory Name</span></div>
        <ul>
            <li><div><img class="expand" src="expand.gif" /><span>Sub Directory Name</span></div></li>
        </ul>
    </li>
</ul>

您可以通过执行以下操作将您的点击绑定到图片:

$('.dir-list img.expand').click(function(){
    //Do stuff here to expand or collapse the area
});
相关问题