Why doesn't my Javascript switch statement work?
I have a switch statement and I must be overlooking something, because I don't see why it wouldn't work.
$('.handles a').click(function() {
var li = $(this).parent();
switch ($(li).data('handle')) {
case 'minimize':
$('.window', li).hide();
开发者_运维问答 break;
}
return false;
});
Also setup a fiddle @ http://jsfiddle.net/9aHvx/4/ (click on Min)
It should hide the .window
div, but it doesn't :(
this line $('.window', li).hide();
is wrong. if you want to target .window and li, you need to write it like you would for css:
$('.window, li').hide();
Edit: Fiddle http://jsfiddle.net/9aHvx/7/
Using the syntax:
$('.window', li).hide();
means that you search for an element with the class window
, starting from the node li
. Objects with the window class that are NOT descendants of li
will NOT be matched.
In your markup, this is exactly the case. You should skip the second parameter all together. Or, you could rearrange your markup (but that is probably not what you want).
Go for:
$('.window').hide();
EDIT: This is if you want to hide the window-element only. If you want to hide the li's as well, then goo for shanethehat's solution.
The .window
div is not inside the li
element, so you don't have to pass the li
variable as parameter:
$('.window').hide();
http://jsfiddle.net/9aHvx/13/
Use $('.window').hide();
instead, because there are no <li>
inside <div class="window">
Your selector is off
$('.handles a').click(function() {
var li = $(this).parent();
var x = $(li).data('handle');
switch (x) {
case 'minimize':
alert(x);
$('.window').hide();
break;
}
return false;
});
jsFiddle link
K sorry I wasted your time.
I just made a major boo boo! :P
$('.handles a').click(function() {
var snippet = $(this).parents('.snippet');
switch($(this).parent().data('handle')) {
case 'minimize':
$('.window', snippet).hide();
break;
}
return false;
});
Thanks all :)
精彩评论