javascript循环:变量等于最后一次迭代
下面的代码将在用户单击addmore链接后添加元素 当用户单击删除链接时,问题就会出现 我的代码里有类似的东西javascript循环:变量等于最后一次迭代,javascript,loops,iteration,Javascript,Loops,Iteration,下面的代码将在用户单击addmore链接后添加元素 当用户单击删除链接时,问题就会出现 我的代码里有类似的东西 <script language="JavaScript"> var count=1; function addmore() { alert(count); var printme = "<table id='table"+count+"'><tr><td><a href='#' onclick='remove(co
<script language="JavaScript">
var count=1;
function addmore() {
alert(count);
var printme = "<table id='table"+count+"'><tr><td><a href='#' onclick='remove(count)'>remove</a></td></tr></table";
//(other code here)...
count++;
}
function remove(y) {
alert(y)
var tab = 'table'+y;
document.getElementById(tab).style.display = "none";
}
</script>
var计数=1;
函数addmore(){
警报(计数);
var printme=“这是因为您将count
作为全局变量
尝试..onclick='remove(“+count+”)..
对值进行“锁定”排序。这是因为您将count
作为全局变量
尝试…onclick='remove(“+count+”)…..
对值进行“锁定”排序。请尝试以下操作:
var printme = "<table id='table"+count+"'><tr><td><a href='#' onclick='remove("+count+")'>remove</a></td></tr></table";
请试试这个:
var printme = "<table id='table"+count+"'><tr><td><a href='#' onclick='remove("+count+")'>remove</a></td></tr></table";
也许只需单击一次class='remove('+count+')即可
你可以这样做
<html>
<head>
<script type="text/javascript">
var count=1;
function addmore() {
var id = 'table' + count;
var table = document.createElement('table');
table.setAttribute('id', id);
var tr = document.createElement('tr');
var td = document.createElement('td');
var a = document.createElement('a');
a.setAttribute('href', '#');
a.appendChild(document.createTextNode('remove ' + id));
a.onclick = function() {
table.style.display = 'none';
document.body.removeChild(table);
};
td.appendChild(a);
tr.appendChild(td);
table.appendChild(tr);
document.body.appendChild(table);
count++;
}
</script>
</head>
<body>
<a href="#" onclick="addmore()">Add Table</a>
</body>
</html>
var计数=1;
函数addmore(){
变量id='表'+计数;
var table=document.createElement('table');
table.setAttribute('id',id);
var tr=document.createElement('tr');
var td=document.createElement('td');
var a=document.createElement('a');
a、 setAttribute('href','#');
a、 appendChild(document.createTextNode('remove'+id));
a、 onclick=function(){
table.style.display='none';
document.body.removeChild(表);
};
td.儿童(a);
tr.appendChild(td);
表1.儿童(tr);
document.body.appendChild(表);
计数++;
}
有了这样定义的表引用和onclick,您不需要id,也许只需要onclick='remove('+count+')
你可以这样做
<html>
<head>
<script type="text/javascript">
var count=1;
function addmore() {
var id = 'table' + count;
var table = document.createElement('table');
table.setAttribute('id', id);
var tr = document.createElement('tr');
var td = document.createElement('td');
var a = document.createElement('a');
a.setAttribute('href', '#');
a.appendChild(document.createTextNode('remove ' + id));
a.onclick = function() {
table.style.display = 'none';
document.body.removeChild(table);
};
td.appendChild(a);
tr.appendChild(td);
table.appendChild(tr);
document.body.appendChild(table);
count++;
}
</script>
</head>
<body>
<a href="#" onclick="addmore()">Add Table</a>
</body>
</html>
var计数=1;
函数addmore(){
变量id='表'+计数;
var table=document.createElement('table');
table.setAttribute('id',id);
var tr=document.createElement('tr');
var td=document.createElement('td');
var a=document.createElement('a');
a、 setAttribute('href','#');
a、 appendChild(document.createTextNode('remove'+id));
a、 onclick=function(){
table.style.display='none';
document.body.removeChild(表);
};
td.儿童(a);
tr.appendChild(td);
表1.儿童(tr);
document.body.appendChild(表);
计数++;
}
使用如下定义的表引用和onclick,您不需要id,前面的所有答案都是正确的,onclick指调用remove时的当前变量计数
为表格生成文本时,请按原样使用count值:
onclick='remove('+count+')...
您可以省略id,并使用以下方法进行计数:
onclick='remove(this.parentElement.parentElement.parentElement);'...
function remove(elementToRemove){
elementToRemove.parentElement.removeChild(elementToRemove);
}
前面的所有答案都是正确的,onclick指调用remove时的当前变量计数
为表格生成文本时,请按原样使用count值:
onclick='remove('+count+')...
您可以省略id,并使用以下方法进行计数:
onclick='remove(this.parentElement.parentElement.parentElement);'...
function remove(elementToRemove){
elementToRemove.parentElement.removeChild(elementToRemove);
}
要添加到这个答案上:您需要立即创建HTML,否则通过闭包保留count
的当前值(最后一个值)。@KenrickChien ooops,这里没有闭包。OP使用全局变量作为形式参数,修复方法是改为使用变量的值(正如目前所有答案所建议的那样)这也消除了出于此目的对全局值的需要。@RobG——我的错误;关于不存在的闭包,您是正确的。若要补充此答案,您需要立即创建HTML,否则将保留当前值(最后一个值)通过闭包计算的count
。@KenrickChien ooops,这里没有闭包。OP使用全局变量作为形式参数,修复方法是使用变量的值(如所有答案所示)这也就不需要全局函数了。@RobG——我的错误;关于一个不存在的闭包,你是对的。