![](/img/trans.png)
[英]How can I get the Id of the particular element of each div / li / a that is created using loop in cshtml
[英]How I Can Get the Id of a li cicked
我有一個從apex 4.2中的數據庫生成的樹:
<ul id="ltr"> <li id="00010" class="open"><a href="" style="" class=""><ins> </ins>Root</a> <ul><li id="00110" class="open"><a href="" style="" class=""><ins> </ins>CHILD</a> <ul><li id="00563" class="open"><a href="" style="" class=""><ins> </ins>CHILD</a> <ul><li id="02057" class="leaf"><a href="" style="" class=""><ins> </ins>LAST CHILD</a></li> </ul></li></ul> </li> </ul> </li> </ul>
我使用單擊功能來選擇節點的ID,而類葉沒有問題,但是使用開放類選擇父節點和父ID,如何選擇所單擊節點的ID?
$(".leaf").click(function(){
alert($(this).attr('id'));
});
$(".open").click(function(){
alert($(this).attr('id'));
});
您需要使用stopPropagation(),否則在單擊內部元素時,事件偵聽器將對所有三個嵌套元素執行。 而且您不應該使用數字作為ID。
這是一個工作中的小提琴:
$(document).ready(function(){ $(".open").click(function(e){ e.stopPropagation(); alert($(this).attr("id")); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <ul id="ltr"> <li id="a00010" class="open"> <a href="" style="" class=""><ins> </ins>Root</a> <ul> <li id="a00110" class="open"><a href="#"><ins> </ins>CHILD</a> <ul> <li id="a00563" class="open"><a href="#"><ins> </ins>CHILD</a> <ul> <li id="a02057" class="leaf"><a href="#"><ins> </ins>LAST CHILD</a></li> </ul> </li> </ul> </li> </ul> </li> </ul>
編輯:如果要選擇.open和.leaf,只需將選擇器更改為:
$(".leaf, .open")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.