简体   繁体   English

使用jQuery TreeView插件时如何触发链接?

[英]How can I trigger a link when using jQuery TreeView Plugin?

I have a JSP page that creates a treeview using jQuery treeview plugin. 我有一个JSP页面,该页面使用jQuery treeview插件创建一个treeview。 Now I want to make the single files selectable, that means I want to open a url when a file is clicked. 现在,我要选择单个文件,这意味着我想在单击文件时打开一个URL。 I tried several example but non of them worked. 我尝试了几个例子,但没有一个奏效。 This is an example html page: 这是一个示例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: 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>

How can I implement this? 我该如何实施?

Hiya demo here : ) http://jsfiddle.net/yeMy9/1/ && http://jsfiddle.net/yeMy9/2/ (only triggers when you click items not the folders. or http://jsfiddle.net/yeMy9/3/ Hiya demo here:) http://jsfiddle.net/yeMy9/1/ && http://jsfiddle.net/yeMy9/2/ (仅当您单击项目而不是文件夹时才触发。或http://jsfiddle.net/ yeMy9 / 3 /

Now I have put the alert when you click the items inside the folder like Item 1.1 etc... and you can use conditional statement to decide base on where to fwd it. 现在,当您单击文件夹中的项(如Item 1.1等)时,我已经放置了警报,您可以使用条件语句来决定在何处转发它。

Cheers and I am sure this will helps! 干杯,我相信这会有所帮助!

Jquery Code 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 的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>​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM