0

我的代码如下所示。当用户单击“下一步”时,我想增加 js 中的计数,并使用固定表达式“文本”和变化变量“计数”的组合作为 var 传递给 innerHTML。我尝试了'text'+count,“'text'+count”等等,这些都没有奏效。有什么建议么?谢谢!!!

<script>
var count = 1
text1 = "feature 1 content"
text2 = "feature 2 content"
text3 = "feature 3 content"

function slideForward()
{
    count = count + 1
    if (count == 4){
        count = 1;}
    document.getElementById('featureShow').innerHTML="text"+count ;
}
</script>

<div id="popUpDivFeatures">
    <p id="featureShow">feature 1 content</p>
    <a href="JavaScript:slideBackward()">Back</a>
    <a href="JavaScript:slideForward()">Next</a> 
</div>
4

2 回答 2

0

您可以稍微更改您的逻辑,例如:

var text = "feature " + count + " content";
于 2013-08-29T03:35:00.160 回答
0

您可以改用数组:

var count = 1

var text = new Array();
text[1] = "feature 1 content";
text[2] = "feature 2 content";
text[3] = "feature 3 content";
function slideForward()
{
    count = count + 1
    if (count == 4){
        count = 1;}
    document.getElementById('featureShow').innerHTML=text[count] ;
}

小提琴:http: //jsfiddle.net/2hewC/

于 2013-08-29T03:54:08.527 回答