[英]Get id of link when id is dynamically generated
我的div中有一堆東西。 我在該div中也有一個鏈接,該鏈接具有動態生成的ID。 無法告訴您ID是什么。
當我單擊該鏈接時,如何將該鏈接的ID轉換為變量jQuery? 在* .js文件中?
嘗試這個,
帶標簽名稱
$('a').click(function(){
//alert($(this).attr('id');
alert(this.id); //Better then the statement above as it is javascript with jQuery wrapper
});
帶班
$('.classOfAnchor').click(function(){
alert($(this).attr('id');
alert(this.id); //Better then the statement above as it is javascript with jQuery wrapper
});
$("a").click(function() {
//alert( $(this).attr('id') );
alert(this.id); //Better then above
});
除非某物為元素提供了ID,否則就不會有ID。
但是,為了便於說明,可以說該鏈接上肯定有一個ID。
如果您使用的是jQuery,則只需選擇其容器div,然后向下瀏覽至鏈接: var myID = $('div#theContainingDiv').children('a').attr('id');
或者,您也可以按照別人說的去做-也可以。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.