开发者

jquery two different trigger with same class

开发者 https://www.devze.com 2023-02-06 19:30 出处:网络
i have two tag, which has same class but different trigger events, following is example: <a class=\"remove\">remove this</a>

i have two tag, which has same class but different trigger events, following is example:

<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)

开发者_高级运维i need to do this in two different triggers, cant do it in same trigger call.


Try this

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


You can test if the clicked anchor's parent's class name is .status and act accordingly, using either .unwrap or .remove:

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

精彩评论

暂无评论...
验证码 换一张
取 消