由于某种原因,我的函数在控制台中给了我一个未定义的错误。为什么是这样? if和else语句在功能上无关紧要;唯一的问题是"我"尽管在switch语句中明确定义了它们,但它永远不会被定义。
$('#nav-em-search, #nav-un-search, #nav-db-search').click(function() {
switch (this) {
case '#nav-em-search':
var i = document.getElementById('em-search');
break;
case '#nav-un-search':
var i = document.getElementById('un-search');
break;
case '#nav-db-search':
var i = document.getElementById('db-search');
break;
}
if (!$('.searchbox').not(i).css('left','-60' + '%')) {
$('.searchbox').not(i).animate({left:'-60' + '%'},500);
setTimeout("$(this).animate({left:'25' + '%'},500)",500);
} else {
$(i).animate({left:'25' + '%'},500);
}
});
答案 0 :(得分:0)
this
设置为单击的元素,而不是元素的ID
您可以在案例之前启用this.id
并删除#
,它应该按预期工作。
switch (this.id) {
case 'nav-em-search':
var i = document.getElementById('em-search');
break;
case 'nav-un-search':
var i = document.getElementById('un-search');
break;
case 'nav-db-search':
var i = document.getElementById('db-search');
break;
}