[英]li jquery on click: if to switch
我正在制作一个简单的过滤系统,其中每个 li 值都有一个特定的描述。 目前它正在使用 IF 语句,但我想将其转换为开关。 这可能吗?
提前致谢。
我的代码:
$("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>");
}
})
这可能吗?
是的,只需使用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()
方法。
switch 语句看起来像这样:
$("ul.simplefilter").on('click', 'li', function (e) {
switch($(this).text()){
case "Iedereen" :
$("#uitleg p").remove();
$("#uitleg").append("<p>Iedereen</p>");
break;
....
附注。 不要忘记break;
在每种情况下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.