2017-10-10 71 views
2

我正在製作一個簡單的過濾系統,其中每個li值都有一定的描述。 目前它正在使用IF語句,但我想將其轉換爲開關。這可能嗎?li jquery點擊:如果要切換

在此先感謝。

我的代碼:

$("ul.simplefilter").on('click', 'li', function (e) { 

if($(this).text() == "Iedereen"){ 
    $("#uitleg p").remove(); 
    $("#uitleg").append("<p>Iedereen</p>");   
} 
else if($(this).text() == "Coach studentenparticipatie"){ 
    $("#uitleg p").remove();  
    $("#uitleg").append("<p>Coach studentenparticipatie</p>"); 
} 
else if($(this).text() == "Communicatie"){ 
    $("#uitleg p").remove();  
    $("#uitleg").append("<p>Communicatie</p>"); 
} 
else if($(this).text() == "Coördinator"){ 
    $("#uitleg p").remove();  
    $("#uitleg").append("<p>Coördinator</p>"); 
} 
}) 
+0

'switch($(this).text)'and case cases'case「Iedereen」:'等 – abhishekkannojia

+0

假設的東西。嘗試:'$('#uitleg')。html('

'+ $(this).text()+'

');' – Tushar

回答

2

這可能嗎?

是的,只需使用switch聲明。

$("ul.simplefilter").on('click', 'li', function (e) { 
    switch($(this).text()){ 
    case "Iedereen": 
     $("#uitleg p").remove(); 
     $("#uitleg").append("<p>Iedereen</p>"); 
     break; 
    case "Coach studentenparticipatie": 
     $("#uitleg p").remove();  
     $("#uitleg").append("<p>Coach studentenparticipatie</p>"); 
     break; 

    } 
}); 

另外,你可以用單線使用.html()方法來實現。

$("ul.simplefilter").on('click', 'li', function (e) { 
    $('#uitleg').html('<p>'+$(this).text()+'</p>'); 
}); 

如果你有,例如,100個值是非常昂貴的寫100的情況下,只需使用.html()方法。

0

switch語句會是這個樣子:

$("ul.simplefilter").on('click', 'li', function (e) { 

switch($(this).text()){ 
    case "Iedereen" : 
     $("#uitleg p").remove(); 
     $("#uitleg").append("<p>Iedereen</p>"); 
     break; 
.... 

PS。不要忘記每種情況下的break;