开发者

Close only target object

开发者 https://www.devze.com 2023-01-12 02:26 出处:网络
I use this quick snippet with an image to close a show/hide DIV: $(\'a.close\').click(function() { $(\'#timeline-2010-1\').hide();

I use this quick snippet with an image to close a show/hide DIV:

  $('a.close').click(function() { 
     $('#timeline-2010-1').hide(); 
     $('#timeline-2010-2').hide();

     return false; 
  });

The problem is that wh开发者_如何学Goen I close one box all of the boxes close...

Is there a way to modify this so that when you click the image "x" on that specific DIV ID, only that one closes, and it doesn't close all of them?


Like this:

$('a.close').click(function() { 
     $(this).closest('.Timeline').hide();

     return false; 
});

$(this).closest('.Timeline') will find the .Timeline element that contains the clicked element. You should replace the .Timeline selector as appropriate.


This code would close the parent div of the image that you're clicking on:

$("div img").click(function(){
  $(this).closest("div").hide();
});

Tied to your DIV IDs, it would be:

$("div[id^='timeline-2010-'] img").click(function(){
  $(this).closest("div").hide();
});


Try something along this way:

  $('a.close').click(function() { 
     $(this).hide(); 
     return false; 
  });

The thing here is the this that refers to that particular object.

0

精彩评论

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