2009-07-13 76 views
0
$("a.close").click(function() { 
    var id = $(this).attr("id"); 
    alert(id); 
    $(this).parents("div.venue:first").fadeOut("Fast"); 
    return false; 
}); 

任何想法爲什麼警報不正確彈出當前的div ID?jQuery ID attr

最終代碼,感謝下面指着我的愚蠢錯誤

var id = $(this).parents("div.venue:first").attr("id"); 

回答

1

因爲你的選擇正在錨:

$("a.close") 

我猜你的意思是你的div警報之後,試試這個:

$("a.close").click(function() { 
    var id = $(this).attr("id"); 
    alert($(this).parents("div.venue:first").attr('id')); 
    return false; 
}); 
0

你是t的元素試圖獲得id是一個不是一個div。

0

你展示如果你想父DIV的id,你需要你的屬性之前遍歷到父<a>元素,而不是一個div

0

的ID。

var id = $(this).closest('div').attr('id');