2012-01-02 40 views
0

我的jQuery:无法使用切换jQuery中

$('#imgCancel').toggle(function() { 
    $.each($("#List li"), function (index, item) { 
     $('#menuList li').removeClass("Selected"); 
    }); 
    $('#txtName').html(''); 
    $('#imgAdd').show(); 
    $('#imgUpdate').hide(); 
}); 
}, function() { //Eroor ---- Expect identifier or string 
    $('#List li:First').addClass("Selected"); 
    $('#imgAdd').hide(); 
    $('#imgUpdate').show(); 
}); 
} 
}); 

但它不是处理toggle.I一种合适的方式得到error.How来执行切换PLZ suggest.Thanks此任务。

+1

你得到了什么错误 – Rafay 2012-01-02 11:50:14

+0

你得到的错误是什么。 – ankur 2012-01-02 11:50:22

+1

你会得到什么错误?你看过jQuery文档:http://api.jquery.com/toggle/ – Pieter 2012-01-02 11:50:31

回答

0
$('#imgCancel').toggle(function() { 
    $.each($("#List li"), function (index, item) { 
     $('#menuList li').removeClass("Selected"); 
    }); 
    $('#txtName').html(''); 
    $('#imgAdd').show(); 
    $('#imgUpdate').hide(); 
}, function() { 
    $('#List li:First').addClass("Selected"); 
    $('#imgAdd').hide(); 
    $('#imgUpdate').show(); 
}); 

每个你可以通过这个,如果你想逃避的第一个

$.each($("#List li"), function (index, item) { 
     if(index != 0){ 
     $('this').removeClass("Selected"); 
} 
    }); 
+0

非常感谢它的完美工作。谢谢。 – 2012-01-02 12:08:35

+0

@ShreeKhanal为什么在第一个匿名函数中有'.each()'? '.each()'函数中的代码对每次迭代都做同样的事情。另外'#List li:First'将不起作用,“First”不需要大写:'#List li:first'。 – Jasper 2012-01-02 12:12:53

+0

@ShreeKhanal不客气, – mgraph 2012-01-02 12:18:47

0

试试这个希望它会解决你有多余的});

$('#imgCancel').toggle(function() { 
     $.each($("#List li"), function (index, item) { 
      $('#menuList li').removeClass("Selected"); 
     }); 
     $('#txtName').html(''); 
     $('#imgAdd').show(); 
     $('#imgUpdate').hide(); 
    }, function() { 
     $('#List li:First').addClass("Selected"); 
     $('#imgAdd').hide(); 
     $('#imgUpdate').show();  
    } 
); 
+0

这也有一个语法错误:/ – Esailija 2012-01-02 11:55:43

+0

ops,thnx,固定 – Rafay 2012-01-02 11:57:58

2
语法错误,更换

看起来你有点过度了}) s:

$('#imgCancel').toggle(//BEGIN TOGGLE 

    function() {//BEGIN FIRST FUNCTION 

     //the each was unnecessary since this selector uses an ID, each iteration was just selecting the same thing over and over 
     $('#menuList li').removeClass("Selected"); 
     $('#txtName').html(''); 
     $('#imgAdd').show(); 
     $('#imgUpdate').hide(); 

    },//END FIRST FUNCTION 

    function() {//BEGIN SECOND FUNCTION 

     //notice here I changed "First" to "first", the capital F was most likely causing this line to not work properly 
     $('#List li:first').addClass("Selected"); 
     $('#imgAdd').hide(); 
     $('#imgUpdate').show(); 

    }//END SECOND FUNCTION 

);//END TOGGLE 
+0

+1因为这至少有一些工作希望大声笑 – Esailija 2012-01-02 12:01:34