节点 select 加载并使 href 协同工作

Node select on load and make href work together

我可以让 work 节点 select on loadmake href 一起工作吗?现在我得到一个无限循环,页面总是刷新。我的代码:

$(document).ready(function () {
    var arrayCollection = <%=GetJson()%>;
    var selectedid = <%=GetSelected()%>;

    $('#jstree').jstree({
        'core': {
            'data': arrayCollection,
        }
    })
    .on('loaded.jstree', function () {
        $('#jstree').jstree('select_node', selectedid);
    })
    .on("select_node.jstree", function (e, data) {
        document.location = data.instance.get_node(data.node, true).children('a').attr('href');
    });
});

如果您要重定向到的页面上有此 javascript。它会无限循环。在加载时它将重新加载树,select 节点并重定向到 selected 节点。

试试这个:

$(document).ready(function () {
    var arrayCollection = <%=GetJson()%>;
    var selectedid = <%=GetSelected()%>;
    $('#jstree')
                .jstree({
                    'core': {
                        'data': arrayCollection,
                    }
                }
                )
                .on('loaded.jstree', function () {
                    $('#jstree').jstree('select_node', selectedid);
                })
                .on("select_node.jstree", function (e, data) {
                    var newLoc = data.instance.get_node(data.node, true).children('a').attr('href');
                    //only redirect if the current location doesn't match the redirect location
                    if(window.location.href != newLoc){
                        document.location = newLoc;
                    }
                });
  });