0

我在sample.html文件中有这样的 HTML 代码:

<ul id='topics' class='filetree'>
<li><span class='folder'><a href='?Lang=en&TopicID=#'>Topics</a></span>
<ul>
<li><span class='folder'><a href='?Lang=en&TopicID=1'>Topic 1</a></span>
<ul>
<li><span class='file'><a href='?Lang=en&TopicID=1.1'>Topic 1.1</a></span></li>
<li><span class='file'><a href='?Lang=en&TopicID=1.2'>Topic 1.2</a></span></li>
<li><span class='file'><a href='?Lang=en&TopicID=1.3'>Topic 1.3</a></span></li>
</ul>
</li>

使用JQuery.load我加载 html 文件以生成 Treeview

<div id="LtrLeftContent">
     <script language="javascript" type="text/javascript">
         $('#LtrLeftContent').load('sample.html', function () {
                $("#topics").treeview();
         });
     </script>
</div>

第一个问题:如何找出当前节点文本。单击树节点时?
第二个问题:如何找出父节点+当前节点文本。单击树节点时?
第三个问题:如何将 CSS 设置为节点。单击树节点时?

经过几次尝试,我得到了当前元素

$("li span").click(function () {
    alert($(this).text());
});

完整代码如下

<div id="LtrLeftContent">
        <script language="javascript" type="text/javascript">  
            $('#LtrLeftContent').load('sample.html', function () {  
                $("#topics").treeview();  
                $("li span").click(function () {  
                    alert($(this).text());  
                });  
            });                 
        </script>  
</div>  
4

1 回答 1

0
$('a').live('click', function (e) {
    e.preventDefault();
    console.log($(e.currentTarget).text());
    console.log($(e.currentTarget).closest('.folder').text());

    // didn't quite get your last question
    // but this is how you would manipulate 
    // the current elements parent list item
    $(e.currentTarget).parents('li').css({
        'background-color': '#000'
    });
    return false;
});
于 2012-07-12T11:13:35.793 回答