2011-10-13 24 views
3

我有如下脚本: 我想包含如下所示的索引值。我怎样才能做到这一点?用我的实际代码,我有错误如何在JavaScript字符串中包含变量

for(var index = 1; index < 6; index++){ 

    $("#myTable thead td:nth-child(index + 2).html("here"); 

} 
+0

你的例子缺少一个括号。你确定你的代码? –

+0

你的代码不起作用,因为你缺少一个关闭括号,原因有一个...... @Curt有正确的答案 – musefan

+0

你的语法缺少一个静物,但我想你可以在html函数中做到这一点:html(var + “
”)? –

回答

0

你可以这样做:

for(var index = 1; index < 6; index++){ 
    var nthchild = index+2; 
    $("#myTable thead td:nth-child("+nthchild+")").html(index); 
} 
+0

+1,因为我看不到投票的理由。 –

+0

@Ash Burlaczenko我认为它得到了编辑(不是我downvoted) – Curt

0

字符串连接,并整理你的语法错误:

for(var index = 1; index < 6; index++) 
    {  
     $("#myTable thead td:nth-child(" + (index+2) + ")").html('here'); 
    } 
0

你必须写:

for(var index = 1; index < 6; index++) { 
    $("#myTable thead td:nth-child("+(index+2)+")").html("here"); 
} 
相关问题