2012-01-02 35 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