jQuery - Close an already open toggle if clicked on
Had a look around on stackoverflow but can't see a similar problem.
I simply want to be able to close the open div that the toggles associated to. At 开发者_JAVA技巧the moment it just replays the toggle effect and slides it open, leaving a div always open/showing.
http://jsfiddle.net/Bx9Ya/
So if clicked, then open associated div. If clicked on again, slide associated div back up.
Like this? http://jsfiddle.net/Bx9Ya/4/ Hide all div's only when toggled element not visible ...
The problem is that before you call toggle() on the one you care to show, you're explicitly hiding ALL of these DIVs with this line of your click function:
$('[class^=toggle-item]').hide();
If you've hidden all, toggling one means to open it.
Check this fiddle where I check the visible state then make a decision based on it: http://jsfiddle.net/JAAulde/Bx9Ya/6/
精彩评论