开发者

help constructing while loop

Hi I am trying to create a while loop and I am having trouble with what to put in it so far I have:

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;
    }
}

It is supposed to be able to loop through the links below and show/hide depending on which link is selected.

<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 开发者_如何学Goid='1link_1c' title="baseball" onclick='showSports(this);'>
  <span>Baseball</span>
</a>


I don't understand your use of the while statement. Perhaps you're thinking of a switch statement.

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;
    }
}


RightSaidFred is correct, it sounds like you meant to use switch. One other point is that the line:

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

will have an index out of bounds error. I think you were thinking to do:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜