Javascript If/Else-If语句缩减
从我的服务器上,我得到一个XML列表,其中包含正在运行的“时间表”。 当时间线处于“运行”或“结束时保持”状态时,按钮将处于活动状态 我已经这样做了,它的工作。 但是我想在我的代码中拒绝使用“Else”语句。 有什么想法吗Javascript If/Else-If语句缩减,javascript,jquery,if-statement,Javascript,Jquery,If Statement,从我的服务器上,我得到一个XML列表,其中包含正在运行的“时间表”。 当时间线处于“运行”或“结束时保持”状态时,按钮将处于活动状态 我已经这样做了,它的工作。 但是我想在我的代码中拒绝使用“Else”语句。 有什么想法吗 function statusCheck() { $.ajax({ type: "GET", url: "/query/timelineStatus?id=1-30", dataType: "xml",
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");
}
});
}
});
}
功能状态检查()
{
$.ajax({
键入:“获取”,
url:“/query/timelineStatus?id=1-30”,
数据类型:“xml”,
成功:函数(xml){
$(xml).find(“timelineStatus”).each(函数(){
var timelineId=parseInt($(this.attr(“id”));
var playState=$(this.find(“playState”).text();
如果(timelineId>0&&timelineId那么动态执行该操作如何
changeJQMThemeSwatch("#timeline_"+timelineId, (playState == "Running" || playState == "Held at end") ? "b" : "a");
如果所有的#timeline_x
值都遵循此模式,只需执行以下操作:
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) { // change as needed
changeJQMThemeSwatch("#timeline_" + timelineId, (playState == "Running" || playState == "Held at end") ? "b" : "a");
}
}
})
});
}
我使用了第一个带有额外“如果”行的选项,效果很好。如果没有“如果”行,它就不起作用。我仍然处于学习曲线中,像这样的小组在这件事上帮助很大。很高兴提供帮助!请记住选择一个答案!:)使用上述技巧是可行的,但在Chrome仪表板中,它显示了拆分字符串的问题:函数changeJQMThemeSwatch(selector,swatch){“var classList=$(selector).attr(“class”).split(/\s+/);“我知道它与/\s+/有关,但我不知道正确的表示法是什么。
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();
switch(timelineId) {
case 1:
changeJQMThemeSwatch("#timeline_1", (playState == "Running" || playState == "Held at end") ? "b" : "a");
break;
case 2:
changeJQMThemeSwatch("#timeline_2", (playState == "Running" || playState == "Held at end") ? "b" : "a");
break;
case 3:
changeJQMThemeSwatch("#timeline_3", (playState == "Running" || playState == "Held at end") ? "b" : "a");
break;
case 4:
changeJQMThemeSwatch("#timeline_4", (playState == "Running" || playState == "Held at end") ? "b" : "a");
break;
case 5:
changeJQMThemeSwatch("#timeline_5", (playState == "Running" || playState == "Held at end") ? "b" : "a");
break;
case 6:
changeJQMThemeSwatch("#timeline_6", (playState == "Running" || playState == "Held at end") ? "b" : "a");
break;
}
}
})
});
}