Javascript为什么这个简单的计数器不在我的循环中工作 [英] Javascript why isnt this simple counter working in my loop
问题描述
我有一个名为button的函数,它使用循环附加m表。我修改了循环,以便为每个< td>
分配一个ID。但是,它似乎没有正常工作。
I have a function that is called button and it appends m table using a loop. I have modified the loop so that it assigns an ID to each <td>
it makes. However, it does not seem to be working right.
这里它正在运行,按照下面的用例进行测试: http://jsfiddle.net/JEAkX/19/
Here it is in action, follow the use case below to test it: http://jsfiddle.net/JEAkX/19/
这是一个简单的我用来测试它的用例:
Here is a simple use case I use to test it:
- 更改单元格中的字母
- 按更多
- 更改其中一个新单元格中的字母
它的功能应该与如果ID被正确分配,则为原始单元格。
It should function the same as the original cells if the ID was getting assigned properly.
计数器为: var n = 13;
并将其插入到附加的单元格中:
and it is inserted into the appended cell as such:
cell.innerHTML = "<input type='text' id='r"+ (n) +"' size='1' onChange='getPos('r"+ (n++) +"'), modifyCells('alphabetTable')' value='" + subset[i++] + "' />"`
这是我得到的DOM来源:
This is the DOM source I am getting:
<td><input id="r13" size="1" onchange="getPos(" r14="" ),="" modifycells(="" alphabettable="" )="" value="q" type="text"></td>
<td><input id="r14" size="1" onchange="getPos(" r15="" ),="" modifycells(="" alphabettable="" )="" value="r" type="text"></td>
我怀疑它与把所有东西塞进1行就像@zzzzBov说的那样但我不知道怎么做否则这样做。
I suspect it has to do with cramming everything into 1 line like @zzzzBov said but I dont know how else to do it.
推荐答案
除了n ++之外,你的HTML中还有一个引用问题。 onchange属性中有嵌套的单引号,如下所示:
Besides the n++, there's a quoting issue in your HTML. There are nested single quotes in the onchange attribute, like so:
< input type ='text'id ='r19'cize =' 1'onChange ='getPos('r20'),modifyCells('alphabetTable')'value ='p'/>
快速修复语法是使用转义双引号,所以你可以开始:
cell.innerHTML =< input type ='text'id ='r+ n +' size ='1'onChange ='getPos(\r+(n ++)+\),modifyCells(\alphabetTable \)'value ='+ subset [i ++] +'/ >中
Quick fix for the syntax is to use escaped double quotes, so you can get going:
cell.innerHTML = "<input type='text' id='r"+ n +"' size='1' onChange='getPos(\"r"+ (n++) +"\"), modifyCells(\"alphabetTable\")' value='" + subset[i++] + "' />"
这篇关于Javascript为什么这个简单的计数器不在我的循环中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!