Javascript li jquery on click:如果要切换
我正在制作一个简单的过滤系统,其中每个li值都有特定的描述。 目前它使用的是IF语句,但我想将其转换为switch。这可能吗 提前谢谢 我的代码:Javascript li jquery on click:如果要切换,javascript,jquery,html,if-statement,switch-statement,Javascript,Jquery,Html,If Statement,Switch Statement,我正在制作一个简单的过滤系统,其中每个li值都有特定的描述。 目前它使用的是IF语句,但我想将其转换为switch。这可能吗 提前谢谢 我的代码: $("ul.simplefilter").on('click', 'li', function (e) { if($(this).text() == "Iedereen"){ $("#uitleg p").remove(); $("#uitleg").append("<p>Iedereen</p>");
$("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>");
}
})
$(“ul.simplefilter”)。在('click','li',函数(e)上{
如果($(this.text()=“Iedereen”){
$(“#uitp”).remove();
$(“#uitleg”)。追加(“Iedereen”);
}
else if($(this).text()=“Coach studentEnterpartitie”){
$(“#uitp”).remove();
$(“#uitleg”)。追加(“Coach StudentEnterpartitie”;
}
else if($(this).text()=“Communicatie”){
$(“#uitp”).remove();
$(“#uitleg”)。追加(“通信””;
}
else if($(this).text()==“协调者”){
$(“#uitp”).remove();
$(“#uitlag”)。追加(“协调者””;
}
})
这可能吗
是的,只需使用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;
}
});
例如,如果您有100个值,那么编写100个案例的成本很高,只需使用
.html()
方法。switch语句如下所示:
$("ul.simplefilter").on('click', 'li', function (e) {
$('#uitleg').html('<p>'+$(this).text()+'</p>');
});
$("ul.simplefilter").on('click', 'li', function (e) {
switch($(this).text()){
case "Iedereen" :
$("#uitleg p").remove();
$("#uitleg").append("<p>Iedereen</p>");
break;
....
$(“ul.simplefilter”)。在('click','li',函数(e)上{
开关($(this).text()){
“Iedereen”案:
$(“#uitp”).remove();
$(“#uitleg”)。追加(“Iedereen”);
打破
....
注意:不要忘记每个案例的break;
。开关($(this.text)
并匹配案例case“Iedereen”:
etcAssuming things.Try:$(''uitleg').html(''+$(this.text()+');