帮助构建while循环

时间:2011-02-07 20:39:27

标签: javascript loops while-loop

您好我正在尝试创建一个while循环,但是到目前为止我遇到了什么问题:

function showSports(obj)
{
    var groupId = obj.id.substring(0, 1);
    var indx = obj.id.substring(obj.id.indexOf('_') + 1);
    var id = indx.substring(0, indx.length + 1);
    var displayInfo = false;
    while (displayInfo)
    {
        if (indx == 1)
        {
            show('footballInfo');
            hide('soccerInfo');
            hide('baseballInfo');
        }
        if (indx == 2)
        {
            show('soccerdInfo');
            hide('baseballInfo');
            hide('footballInfo');
        }
        if (indx == 3)
        {
            show('baseballInfo');
            hide('footballInfo');
            hide('soccerdInfo');
        }
        displayInfo = true;
    }
}

它应该能够遍历下面的链接并显示/隐藏,具体取决于选择的链接。

<a id='1link_1a' title="football Tab" onclick='showSports(this);'>
  <span>FootBall</span>
</a>
<a id='1link_1b' title="soccer"
onclick='showSports(this); changeTab(this);'>
  <span>Soccer</span>
</a>
<a id='1link_1c' title="baseball" onclick='showSports(this);'>
  <span>Baseball</span>
</a>

2 个答案:

答案 0 :(得分:1)

我不明白你对while语句的使用。也许你正在考虑转换声明。

function showSports(obj)
{
    var groupId = obj.id.substring(0, 1);
    var indx = obj.id.substring(obj.id.indexOf('_') + 1);
    var id = indx.substring(0, indx.length + 1);

    switch (indx)
    {
        case 1:
            show('footballInfo');
            hide('soccerInfo');
            hide('baseballInfo');
        break;
        case 2:
            show('soccerdInfo');
            hide('baseballInfo');
            hide('footballInfo');
        break;
        case 3:
            show('baseballInfo');
            hide('footballInfo');
            hide('soccerdInfo');
        break;
    }
}

答案 1 :(得分:0)

RightSaidFred是正确的,听起来你打算使用开关。另一点是该行:

  

var id = indx.substring(0,indx.length + 1);

将出现索引超出范围的错误。我想你想要这样做:

  

var id = indx.substring(0,indx.length - 1);