<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>判断</title>
<style type="text/css">
*{
margin:0;
padding:0;
}
.div1{width:200px; height:30px; background:#069; margin-top:20px;}
.div2{width:50px;height:30px; background:#00BCF3;line-height:30px;text-align:center;cursor:pointer;}
</style>
</head>
<body>
<div style="margin:100px 500px;">
<input id="v_input" type="text" value="1" /><br />
<div id="zj_div">
<div class="div1">
<a style="float:right;" href="javascript:void(0)" onclick="del(this)">删除</a>
</div></div>
<div id="btn_div" class="div2">增加</div>
</div>
</body>
<script type="text/javascript">
var i= 0,oTab;
window.onload=function(){
oTab = document.getElementById('zj_div');
document.getElementById('btn_div').onclick=function(){
oTab.appendChild(oTab.children[0].cloneNode(true));
i++;
}
};
function del(obj){
i&&oTab.removeChild(obj.parentNode)&&i--;
}
</script></html> 请问 怎么判断input的value的值 来控制我要增加div的数量 当我输入2的时候 下面的只能添加两个div 当我想创建第3个div的时候 增加这个按钮就不能点或者创建了
getElementById(id).value
之后for循环
appendChild试试
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
function ss(){
var v=document.getElementById("test").value;
if(!/^\d+$/.test(v)){
alert("只能是正整数");
return false;
}
for(var i=1;i<=v;i++){
var div=document.createElement("div");
div.innerHTML=i;
document.body.appendChild(div);
}
}
</script>
</head><body>
<input type="text" id="test" />
<input type="button" onclick="ss()" value="add"/>
</body>
</html>
类似这样试试
var i= 0,oTab;
window.onload=function(){
oTab = document.getElementById('zj_div');
document.getElementById('btn_div').onclick=function(){
var addedNum = Number(document.getElementById("v_input").value);
if(!/^\d+$/gi.test(addedNum)) {
return false;
}
for(var j = 0; j < addedNum; j++) {
oTab.appendChild(oTab.children[0].cloneNode(true));
i++;
}
}
};
function del(obj){
i&&oTab.removeChild(obj.parentNode)&&i--;
}