带有JavaScript的onclick属性中的变量范围
我正在使用带有JavaScript的onclick属性中的变量范围,javascript,variables,prototype,Javascript,Variables,Prototype,我正在使用Prototype并尝试动态访问循环中的变量 代码比我说得好: for(var i=0;i您可以这样做- for (var i = 0; i < 5; i++) { $('parent' + i).insert('<input type="button" value="Cancel" onclick="$(\'parent' + i + '\').remove()" />', {position: 'after'}); } <input type="
Prototype
并尝试动态访问循环中的变量
代码比我说得好:
for(var i=0;i您可以这样做-
for (var i = 0; i < 5; i++) {
$('parent' + i).insert('<input type="button" value="Cancel" onclick="$(\'parent' + i + '\').remove()" />', {position: 'after'});
}
<input type="button" value="Cancel" onclick="$('parent1').remove()" />
<input type="button" value="Cancel" onclick="$('parent2').remove()" />
<input type="button" value="Cancel" onclick="$('parent3').remove()" />
...
for(变量i=0;i<5;i++){
$('parent'+i).insert('',{position:'在'}之后);
}
它将被渲染成这样-
for (var i = 0; i < 5; i++) {
$('parent' + i).insert('<input type="button" value="Cancel" onclick="$(\'parent' + i + '\').remove()" />', {position: 'after'});
}
<input type="button" value="Cancel" onclick="$('parent1').remove()" />
<input type="button" value="Cancel" onclick="$('parent2').remove()" />
<input type="button" value="Cancel" onclick="$('parent3').remove()" />
...
...
等等。你可以这样做-
for (var i = 0; i < 5; i++) {
$('parent' + i).insert('<input type="button" value="Cancel" onclick="$(\'parent' + i + '\').remove()" />', {position: 'after'});
}
<input type="button" value="Cancel" onclick="$('parent1').remove()" />
<input type="button" value="Cancel" onclick="$('parent2').remove()" />
<input type="button" value="Cancel" onclick="$('parent3').remove()" />
...
for(变量i=0;i<5;i++){
$('parent'+i).insert('',{position:'在'}之后);
}
它将被渲染成这样-
for (var i = 0; i < 5; i++) {
$('parent' + i).insert('<input type="button" value="Cancel" onclick="$(\'parent' + i + '\').remove()" />', {position: 'after'});
}
<input type="button" value="Cancel" onclick="$('parent1').remove()" />
<input type="button" value="Cancel" onclick="$('parent2').remove()" />
<input type="button" value="Cancel" onclick="$('parent3').remove()" />
...
...
诸如此类。我认为您将I
放在带引号的字符串中,而不是parent
中,因此它在错误的范围级别(从概念上讲)
for(var i=0;i我认为您将i
放在带引号的字符串中,而不是parent
中,因此它处于错误的范围级别(从概念上讲)
for(var i=0;i不需要该i变量。如果要删除该输入的父项,请执行以下操作:
<input type="button" value="Cancel" onclick="this.parentNode.parentNode.removeChild(this.parentNode)" />
或者,如果您确实想要,您可以:
for (var i=0;i<5;i++) {
$('parent' + i).insert('<input type="button" value="Cancel" onclick="$(\'parent' + i + '\').remove()" />', {position: 'after'});
}
for(var i=0;i不需要该i变量。如果要删除该输入的父项,请执行以下操作:
<input type="button" value="Cancel" onclick="this.parentNode.parentNode.removeChild(this.parentNode)" />
或者,如果您确实想要,您可以:
for (var i=0;i<5;i++) {
$('parent' + i).insert('<input type="button" value="Cancel" onclick="$(\'parent' + i + '\').remove()" />', {position: 'after'});
}
for(var i=0;我不是必须为原型添加'whiledoing$('parent1')吗?他必须使用\'来转义它。我们不是必须为原型添加'whiledoing$('parent1')吗?他必须使用\'来转义它。