2011-01-23 36 views
2

我有兩個標籤,其中有同一類,但不同的觸發事件,以下是例子:jQuery的兩個不同的觸發與同一類

<a class="remove">remove this</a> 
<div class="status"><a class="remove">Remove this status div only</a></div> 

in jquery i have it like 
$(".remove").live('click', function()... (this gets trigger for both) 
$(".status_update > .remove").live('click', function()... (i want this to trigger for status div remove link) 

我需要做的這兩個不同的觸發器,不能做它在同觸發呼叫。

回答

1

試試這個

$(".remove").live('click', function() { 
    if ($(this).parent().hasClass('status_update')) // execute inner links code 
    else // execute outer links code 
}); 
1

您可以測試是否點擊的錨的父類名是.status並採取相應的行動,無論是使用.unwrap.remove

$("a.remove").live('click', function() { 
    if($(this).parent().hasClass("status")) { // or $(this).parent('.status').length 
     $(this).unwrap("div.status"); 
    } else { 
     $(this).remove(); 
    } 
}); 
+0

感謝卡里姆的答案,但我需要要在兩個不同的調用中做到這一點,不能在一個調用中做到這一點,因爲一個是全局的,另一個是子部分,其改變爲每個不同的頁面。 – Basit 2011-01-23 12:10:43

+0

@Basit - 我有點失落。你的意思是`.unwrap`和`.remove`是你需要的,只有在你的問題中的兩個事件處理程序?或者還有其他一些問題? – karim79 2011-01-23 12:17:14

相關問題