Frage

HTML structure looks like this: title > ul > li > a. After clicking on the anchor tag, I need to .slideToggle() on the .content element, which is closest to the .title.

My current js for it looks like this and it doesn't work. Here's a fiddle

$(function(){ 
  $('.title > ul > li > a').on('click', function(){
    $(this).closest('.content').slideToggle();
  })  
})
War es hilfreich?

Lösung

.content is not in the same tree as your selector, therefore .closest wont find it. You need to use a combo of .closest and .next

$(this).closest('.title').next(".content").slideToggle();

Fiddle: http://jsfiddle.net/ad9bz/5/

Also, be sure to include jQuery in your fiddle next time!

Andere Tipps

How about:

$(this).parents('.title').next('.content').slideToggle();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top