Json结果作为链接

时间:2014-06-16 10:08:59

标签: jquery json

因为我在javascript和json上有0个背景,所以使用这个tree demo from jeasyui

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Animation Tree - jQuery EasyUI Demo</title>
    <link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../../themes/icon.css">
    <link rel="stylesheet" type="text/css" href="../demo.css">
    <script type="text/javascript" src="../../jquery.min.js"></script>
    <script type="text/javascript" src="../../jquery.easyui.min.js"></script>
</head>
<body>
    <h2>Animation Tree</h2>
    <p>Apply 'animate' property to true to enable animation effect.</p>
    <div style="margin:20px 0;"></div>
    <div class="easyui-panel" style="padding:5px">
        <ul class="easyui-tree" data-options="url:'tree_data1.json',method:'get',animate:true"></ul>
    </div>
</body>
</html>

如何将结果显示为可点击链接?使用带<a href="somelink"> click me </a>的树的东西,我的意思是,让json数据的每一项在点击时打开一个url。有点像,例如:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Basic Tree - jQuery EasyUI Demo</title>
    <link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../../themes/icon.css">
    <link rel="stylesheet" type="text/css" href="../demo.css">
    <script type="text/javascript" src="../../jquery.min.js"></script>
    <script type="text/javascript" src="../../jquery.easyui.min.js"></script>
</head>
<body>
    <h2>Basic Tree</h2>
    <p>Click the arrow on the left to expand or collapse nodes.</p>
    <div style="margin:20px 0;"></div>
    <div class="easyui-panel" style="padding:5px">
        <ul class="easyui-tree">
            <li>
                <span>My Documents</span>
                <ul>
                    <li data-options="state:'closed'">
                        <span>Photos</span>
                        <ul>
                            <li><a href="www.google.com">Google</a></li>
                            <li><a href="www.altavista.com">Altavista</a></li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
    </div>

</body>
</html>

提前致谢。

1 个答案:

答案 0 :(得分:2)

我能够通过使用:

来解决它
            <ul class="easyui-tree"
                data-options="
                    url:'jj.json',
                    method:'get',
                    animate:true,
                    formatter:function(node){
                        var s = node.text;
                        if (node.server){
                            s = '<a href=\'http://'+ node.server +'\'>' + node.server + '</a>';
                        }
                        return s;
                    }
                ">
            </ul>

json的内容:

[{
    "id":1,
    "text":"Environment",
    "children":[{
        "id":11,
        "text":"ENV",
        "state":"closed",
        "children":[{
            "id":111,
            "server":"localhost"
        },{
            "id":112,
            "server":"server b"
        },{
            "id":113,
            "server":"server c"
        }]
    }]
}]