2011-09-14 53 views
2

我正在嘗試使用JSON生成的jsTree來導航目錄結構。目前,我綁定了一個select_node事件來獲取選定節點的路徑作爲字符串,然後將location.hash設置爲該路徑。這部分實際上工作正常。我的問題是,在我的select_node事件完成之後,立即從url中完全刪除哈希,這顯然會中斷瀏覽器歷史並將用戶發送回「索引」頁面。這是我目前的代碼。我如何防止這種情況發生?jsTree從URL中刪除哈希

$('#projects').jstree({ 
    core: { 
    animation: 0 
    }, 
    plugins: ["themes", "json_data", "ui"], 
    themes: { 
    theme: "gm", 
    dots: false 
    }, 
    json_data: { 
    ajax: { 
     url: '/json/projects', 
    }, 
    progressive_render: true 
    } 
}).bind('select_node.jstree', function(e, data){ 
    var path = '#/' + $(this).jstree('get_path', data.rslt.obj, false).join('/') 
    window.location.hash = path 
}) 
+0

我也想知道這一點。明天我會有同樣的問題:) –

回答

0

我認爲這個問題的代碼是這樣的:

var path = '/' + $(this).jstree('get_path', data.rslt.obj, false).join('/') 

你或許可以使用

var path = '/' + $(this).jstree('get_path', data.rslt.obj, false).join('/') + location.hash 

,以確保哈希依然存在。

+0

對不起,我在我的例子中有一個錯字。 'path'實際上應該是'window.location.hash'被設置爲哪個實際發生的好。問題是馬上就會有其他事情在jstree中恢復爲無。 – wdh

+0

你可以使用location.search嗎?也許jstree需要使用location.hash –

+0

我使用ajax來拉動基於哈希的目錄列表,所以沒有,我不能。我或者需要停止jstree自動修改哈希(這與我目前的設置沒有任何關係,因爲沒有實際的鏈接),或者找到其他方法讓每個項目鏈接到它的路徑。 – wdh