在JQuery Treeview中将Click事件分配给

时间:2022-08-24 12:40:04

I am using the JQuery Treeview plugin to display some data. Basically, I would like to add a click event to the child < li > elements that copies their innerhtml into another div on the page. I have not been able to assign a click event to these < li > elements however.

我正在使用JQuery Treeview插件来显示一些数据。基本上,我想在子

  • 元素中添加一个click事件,将其innerhtml复制到页面上的另一个div中。但是,我无法为这些
  • 元素分配点击事件。

  • Hoping someone has tread this ground before and can provide some help.

    希望有人在此之前踩过这片土地并提供一些帮助。

    Thanks.

    4 个解决方案

    #1


    Using the markup from the example at http://docs.jquery.com/Plugins/Treeview:

    使用http://docs.jquery.com/Plugins/Treeview上示例中的标记:

    $("span.file, span.folder", "#example li")
        .click(function() { alert($(this).text()); });
    

    works. Handling the click on the LI items themselves captures branch contractions and expansion.

    作品。处理LI项目本身的点击会捕获分支收缩和扩展。

    #2


    You can use .live construction. For me it works:

    您可以使用.live构造。对我来说它有效:

    $('li','ul#menu').live('click', function(){
        alert('Click event');    
    });
    

    #3


    <li onclick="selectNode(event, this);" id="${node2.nodeId}" class="closed">
        <span class="folder"> ${node2.name} </span>
    </li>
    
    
    
    
    function selectNode(event, nodeHtmlEl) {
        // IE
        if ($.browser.msie) {
            window.event.cancelBubble = true; 
        }
        if(event.stopPropagation) { 
            event.stopPropagation();
        }
        alert("selectNode ID: " + $(nodeHtmlEl).attr("id"));
    }
    

    #4


    Try like this

    试试这样吧

    $('li:not(:has(>ul))', 'ul#menu').live('click', function () {             
                    alert($(this).text());
                });
    

    it will add click event to leaf node only (not root node)

    它会将click事件添加到叶节点(不是根节点)

    #1


    Using the markup from the example at http://docs.jquery.com/Plugins/Treeview:

    使用http://docs.jquery.com/Plugins/Treeview上示例中的标记:

    $("span.file, span.folder", "#example li")
        .click(function() { alert($(this).text()); });
    

    works. Handling the click on the LI items themselves captures branch contractions and expansion.

    作品。处理LI项目本身的点击会捕获分支收缩和扩展。

    #2


    You can use .live construction. For me it works:

    您可以使用.live构造。对我来说它有效:

    $('li','ul#menu').live('click', function(){
        alert('Click event');    
    });
    

    #3


    <li onclick="selectNode(event, this);" id="${node2.nodeId}" class="closed">
        <span class="folder"> ${node2.name} </span>
    </li>
    
    
    
    
    function selectNode(event, nodeHtmlEl) {
        // IE
        if ($.browser.msie) {
            window.event.cancelBubble = true; 
        }
        if(event.stopPropagation) { 
            event.stopPropagation();
        }
        alert("selectNode ID: " + $(nodeHtmlEl).attr("id"));
    }
    

    #4


    Try like this

    试试这样吧

    $('li:not(:has(>ul))', 'ul#menu').live('click', function () {             
                    alert($(this).text());
                });
    

    it will add click event to leaf node only (not root node)

    它会将click事件添加到叶节点(不是根节点)