gpt4 book ai didi

javascript - If/Else if语句缩减

转载 作者:行者123 更新时间:2023-11-30 17:33:57 25 4
gpt4 key购买 nike

从我的服务器,我得到了一个 XML 列表,其中包含正在运行的“时间线”。当时间轴处于“运行”或“结束时保持”状态时,按钮将处于事件状态。

我是这样做的,效果很好。但我想拒绝代码中的“Else”语句。有什么想法吗?

 function statusCheck()
{
$.ajax({
type: "GET",
url: "/query/timelineStatus?id=1-30",
dataType: "xml",
success: function (xml) {
$(xml).find("timelineStatus").each(function(){
var timelineId = parseInt($(this).attr("id"));
var playState = $(this).find("playState").text();
if (timelineId == 1) // timeline_1
{
changeJQMThemeSwatch("#timeline_1", (playState == "Running" || playState == "Held at end") ? "b" : "a");
}
else if (timelineId == 2) // timeline_2
{
changeJQMThemeSwatch("#timeline_2", (playState == "Running" || playState == "Held at end") ? "b" : "a");
}
else if (timelineId == 3) // timeline_3
{
changeJQMThemeSwatch("#timeline_3", (playState == "Running" || playState == "Held at end") ? "b" : "a");
}
else if (timelineId == 4) // timeline_4
{
changeJQMThemeSwatch("#timeline_4", (playState == "Running" || playState == "Held at end") ? "b" : "a");
}
else if (timelineId == 5) // timeline_5
{
changeJQMThemeSwatch("#timeline_5", (playState == "Running" || playState == "Held at end") ? "b" : "a");
}
else if (timelineId == 6) // timeline_6
{
changeJQMThemeSwatch("#timeline_6", (playState == "Running" || playState == "Held at end") ? "b" : "a");
}

}
})
}

最佳答案

根据您的 id 命名的标准,您可以只使用一个小的字符串连接。

function statusCheck()
{
$.ajax({
type: "GET",
url: "/query/timelineStatus?id=1-30",
dataType: "xml",
success: function (xml) {
$(xml).find("timelineStatus").each(function(){
var timelineId = parseInt($(this).attr("id"));
var playState = $(this).find("playState").text();
if (timelineId > 0 && timelineId <= 6) {
changeJQMThemeSwatch("#timeline_" + timelineId, (playState == "Running" || playState == "Held at end") ? "b" : "a");
}
});
}
});
}

关于javascript - If/Else if语句缩减,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22428198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com