0

我有一个使用 jQuery 树视图插件创建树视图的 JSP 页面。现在我想让单个文件可选择,这意味着我想在单击文件时打开一个 url。我尝试了几个示例,但没有一个有效。这是一个示例 html 页面:

<ul id="browser" class="filetree">
    <li><span class="folder">Folder 1</span>
        <ul>
            <li><span class="file">Item 1.1</span></li>
        </ul>
    </li>
    <li><span class="folder">Folder 2</span>
        <ul>
            <li><span class="folder">Subfolder 2.1</span>
                <ul id="folder21">
                    <li><span class="file">File 2.1.1</span></li>
                    <li><span class="file">File 2.1.2</span></li>
                </ul>
            </li>
            <li><span class="file">File 2.2</span></li>
        </ul>
    </li>
    <li class="closed"><span class="folder">Folder 3 (closed at start)</span>
        <ul>
            <li><span class="file">File 3.1</span></li>
        </ul>
    </li>
    <li><span class="file">File 4</span></li>
</ul>

JavaScript:

<script type="text/javascript">
    $(document).ready(function(){
        $("#browser").treeview({
            toggle: function() {
                console.log("%s was toggled.", $(this).find(">span").text());
                alert("do something");
            }
        });

        // fourth example
        $("#black, #gray").treeview({
            control: "#treecontrol",
            persist: "cookie",
            //cookieId: "treeview-black"
        });
    });
</script>

我该如何实施?

4

1 回答 1

1

Hiya演示在这里:) http://jsfiddle.net/yeMy9/1/ && http://jsfiddle.net/yeMy9/2/(仅在您单击项目而不是文件夹时触发。或 http://jsfiddle.net/ yeMy9/3/

现在,当您单击文件夹内的项目(如 Item 1.1 等)时,我已发出警报……您可以使用条件语句来决定将其转发到何处。

干杯,我相信这会有所帮助!

jQuery代码

 $(document).ready(function(){
        $("#browser").treeview({
            toggle: function() {
                console.log("%s was toggled.", $(this).find(">span").text());
                alert("do something");

            }
        });

      $("li span").click(function(){
          alert(' Forward the url when clicked => ' + $(this).text());
          //Now forward to the link according to the text() i.e. based
          // on Item 1.1 & 2.2 etc...
      });

        // fourth example
        $("#black, #gray").treeview({
            control: "#treecontrol",
            persist: "cookie",
            //cookieId: "treeview-black"
        });
    });​

HTML

<ul id="browser" class="filetree">
    <li><span class="folder">Folder 1</span>
        <ul>
            <li><span class="file">Item 1.1</span></li>
        </ul>
    </li>
    <li><span class="folder">Folder 2</span>
        <ul>
            <li><span class="folder">Subfolder 2.1</span>
                <ul id="folder21">
                    <li><span class="file">File 2.1.1</span></li>
                    <li><span class="file">File 2.1.2</span></li>
                </ul>
            </li>
            <li><span class="file">File 2.2</span></li>
        </ul>
    </li>
    <li class="closed"><span class="folder">Folder 3 (closed at start)</span>
        <ul>
            <li><span class="file">File 3.1</span></li>
        </ul>
    </li>
    <li><span class="file">File 4</span></li>
</ul>​
于 2012-04-16T10:04:40.153 回答