简体   繁体   English

li jquery on click:如果切换

[英]li jquery on click: if to switch

I'm making a simple filter system where each li value has a certain description.我正在制作一个简单的过滤系统,其中每个 li 值都有一个特定的描述。 Currently it's working with an IF statement, but I'd like to convert it to a switch.目前它正在使用 IF 语句,但我想将其转换为开关。 Is this possible?这可能吗?

Thanks in advance.提前致谢。

My code:我的代码:

$("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>");
}
})

Is this possible?这可能吗?

Yes, just use switch statement.是的,只需使用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;

   }
});

Also, you can achieve that with single line using .html() method.此外,您可以使用.html()方法通过单行实现这一点。

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

If you have, for instance, 100 values it is expensive to write 100 cases, just use .html() method.例如,如果您有 100 个值,则编写 100 个案例的成本很高,只需使用.html()方法。

The switch statement will look something like this: switch 语句看起来像这样:

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

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

PS.附注。 Don't forget the break;不要忘记break; on each case.在每种情况下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM