Javascript 通过输入字段的值控制HTML元素的数量
我正在尝试构建一个基于JavaScript的布局网格生成器,其中我希望通过输入字段的值来控制网格行的数量。例如,如果我键入Javascript 通过输入字段的值控制HTML元素的数量,javascript,html,for-loop,Javascript,Html,For Loop,我正在尝试构建一个基于JavaScript的布局网格生成器,其中我希望通过输入字段的值来控制网格行的数量。例如,如果我键入12,我的容器-div中应该有12个div。我已经做了一个函数可以处理这个 document.getElementById('numofrows').onkeyup=function(){ var foo = []; for (var i=0; i<this.value ;i++) { foo[i] = document.createEl
12
,我的容器-div
中应该有12个div。我已经做了一个函数可以处理这个
document.getElementById('numofrows').onkeyup=function(){
var foo = [];
for (var i=0; i<this.value ;i++) {
foo[i] = document.createElement('div');
container.appendChild(foo[i]);
}
}
document.getElementById('numorrows').onkeyup=function(){
var foo=[];
对于(var i=0;ivar maxNumber=this.value;
对于(var i=0;ivar maxNumber=this.value;
对于(var i=0;i而言,这不是解决问题的最佳方法,但是
if('div'.Count >= MaxNumber){
Do Not Add Child.
}else{
Add Child.
}
这不是解决问题的最好办法,但是
if('div'.Count >= MaxNumber){
Do Not Add Child.
}else{
Add Child.
}
您可以先清除子div,然后添加新列表;如:
document.getElementById('numofrows').onkeyup = function () {
var foo = [];
// remove all the children of the parent element container
while (container.firstChild) container.removeChild(container.firstChild);
for (var i = 0; i < this.value; i++) {
foo[i] = document.createElement('div');
container.appendChild(foo[i]);
}
}
document.getElementById('numorrows').onkeyup=function(){
var foo=[];
//删除父元素容器的所有子元素
while(container.firstChild)container.removeChild(container.firstChild);
对于(var i=0;i
演示:您可以先清除子div,然后添加新列表;如:
document.getElementById('numofrows').onkeyup = function () {
var foo = [];
// remove all the children of the parent element container
while (container.firstChild) container.removeChild(container.firstChild);
for (var i = 0; i < this.value; i++) {
foo[i] = document.createElement('div');
container.appendChild(foo[i]);
}
}
document.getElementById('numorrows').onkeyup=function(){
var foo=[];
//删除父元素容器的所有子元素
while(container.firstChild)container.removeChild(container.firstChild);
对于(var i=0;i
演示:如果要使其动态化,每次执行函数时,只需清除容器div并添加与输入值对应的div数。如下所示:
document.getElementById('numofrows').onkeyup=function(){
document.getElementById('container').innerHTML='';
var foo = [];
for (var i=0; i<this.value ;i++) {
foo[i] = document.createElement('div');
container.appendChild(foo[i]);
}
}
document.getElementById('numorrows').onkeyup=function(){
document.getElementById('container')。innerHTML='';
var foo=[];
对于(var i=0;i如果要使其成为动态的,则每次执行函数时,只需清除容器div并添加与输入值对应的div数。如下所示:
document.getElementById('numofrows').onkeyup=function(){
document.getElementById('container').innerHTML='';
var foo = [];
for (var i=0; i<this.value ;i++) {
foo[i] = document.createElement('div');
container.appendChild(foo[i]);
}
}
document.getElementById('numorrows').onkeyup=function(){
document.getElementById('container')。innerHTML='';
var foo=[];
for(var i=0;我非常感谢你!就像一个咒语。我也喜欢其他答案,但这一个似乎是最干净的。非常感谢!就像一个咒语。我也喜欢其他答案,但这一个似乎是最干净的。