简体   繁体   English

使用jQuery在嵌套的UL LI结构中查找LI元素的ID

[英]Find id of an LI element in a nested UL LI structure using jQuery

I have the following sample HTML code: http://pastebin.com/Mya6tPc6 And here is the jQuery code I am trying to use: 我有以下示例HTML代码: http : //pastebin.com/Mya6tPc6这是我尝试使用的jQuery代码:

$("#folder1").click(function(){
 alert($(this).attr('id'));
});

$("#f1").click(function(){
 alert($(this).attr('id'));
});

Here is the problem: If the user clicks on the element 'folder1', it will display 'folder1' in the alert box. 这是问题所在:如果用户单击元素“ folder1”,它将在警报框中显示“ folder1”。 That is fine. 那也行。 If the user clicks on 'folder1.1', it will display 'folder1' in the alert box, actually I don't want it to do anything at this point. 如果用户单击“ folder1.1”,它将在警报框中显示“ folder1”,实际上我现在不希望它做任何事情。 If the user clicks on 'f1', it will display first an alert box with 'f1' and then another alert box with 'folder1'. 如果用户单击“ f1”,它将首先显示带有“ f1”的警报框,然后显示带有“ folder1”的另一个警报框。 I need it to display only one alert box with 'f1'. 我需要它仅显示一个带有'f1'的警报框。

Would someone please tell me how to do this? 有人可以告诉我该怎么做吗? The structure is a directory tree, so it can have unlimited number of depth. 该结构是目录树,因此它可以具有无限数量的深度。 I need to set it up in a way that when the user clicks on a folder LI, a function will display the contents of that folder in another area. 我需要以一种方式设置它,即当用户单击文件夹LI时,一个函数将在另一个区域中显示该文件夹的内容。

I know there are other file managers but I need to make one by myself in this case. 我知道还有其他文件管理器,但是在这种情况下,我需要自己制作一个。

I have searched for 'jQuery and nested UL' and the sort in google, but till now I haven't found a solution. 我已经搜索了“ jQuery和嵌套UL”以及google中的排序方式,但是到目前为止,我还没有找到解决方案。 Did I search for the wrong term? 我搜索了错误的词吗?

Thank you very much for your answers. 非常感谢您的回答。

Best regards, Tony. 最好的问候,托尼。

Not completely sure what you want still, but sounds like you're having issues with event bubbling. 不确定您仍然想要什么,但听起来好像事件冒泡有问题。 Meaning that, if you click a nested element, the event will first fire on that element, then on it's parent, until reaching the top-level element in your document. 这意味着,如果单击嵌套元素,则该事件将首先在该元素上触发,然后在其父元素上触发,直到到达文档中的顶级元素。 You could: 你可以:

1) cancel the event after handling the click on the child (#f1); 1)处理完对子项的点击后取消事件(#f1);

$("#f1").click(function( e ){
    e.preventDefault();
    alert($(this).attr('id'));
    return false;
});

2) check if the target of the event is the element you're handling the event for currently; 2)检查事件的目标是否是您当前正在处理事件的元素; see event object 查看事件对象

$("#folder1").click(function( e ){
    if ( e.target == e.currentTarget ) {
        // do something
    }
});

(The comment box doesn't allow me to post this message, so I am using the answer box. I hope this is not a problem.) (评论框不允许我发布此消息,因此我正在使用答案框。我希望这不是问题。)

Hello Paul, 保罗你好

Thank you for your comments. 谢谢您的意见。 The following worked well: 以下效果很好:

var i=0;

$("#folder1").click(function( e ){
    $("#files").append(++i + ". " + $(this).attr('id')+"<br>");
    return false;
});

$("#folder1_1").click(function( e ){
    var me = e.currentTarget;
    $("#files").append(++i + ". " + $(this).attr('id')+"<br>");
    return false;
});

$("#f1").click(function( e ){
    $("#files").append(++i + ". " + $(this).attr('id')+"<br>");
    return false;
});

The i is only for testing purpose. i仅用于测试目的。

The e.preventDefault(); e.preventDefault(); didn't work well with the jQuery Treeview plugin. 不能与jQuery Treeview插件配合使用。 So I had to remove it. 所以我不得不将其删除。 Seems like the return false; 好像返回false; does the job. 做这份工作。

This: 这个:

if ( e.target == e.currentTarget ) ... May I know if you get a true from this comparison in any case? if(e.target == e.currentTarget)...我是否可以在任何情况下从此比较中得出真假?

I can get it to work with the following: 我可以将其与以下各项一起使用:

    if(e.currentTarget === this)
    {
        alert("Its me!");
    }

I am not sure if this will work on all browers, on Safari and Chrome this looks ok. 我不确定这是否适用于所有浏览器,在Safari和Chrome上都可以。

Thanks a lot for the ideas and information on things like event bubbling too. 非常感谢您对事件冒泡之类的想法和信息。

Best regards, Tony. 最好的问候,托尼。

Hello Yi Jiang, 江艺你好

Thank you, you are right. 谢谢,你是对的。 I will do that from now on. 我将从现在开始。

Best regards, Tony. 最好的问候,托尼。

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

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