2017-10-10 75 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;