动态添加button的问题 js 动态添加多个button 而且每个button 都有相同的事件 哪位大侠能写个简单的实例出来,在线等!!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>c.html</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><script type="text/javascript">function add() { var divBox = document.createElement("div"); divBox.innerHTML = "<input type='button' value='button' onclick='test();'/>"; document.getElementById("show").appendChild(divBox); }function test(){ alert("this is a test.");}</script></head><body><input type="button" value="添加" onClick="add()"><div id="show"></div> </body></html> <html> <head> <script type="text/javascript"> var test = function(){ alert("我被创建了"); } function addButton(){ var count = parseInt(document.myform.count.value); for(var i = 0;i<count;i++){ var newButton = document.createElement("input"); newButton.setAttribute("type","button"); newButton.setAttribute("value","button"+i); newButton.onclick = test; document.myform.appendChild(newButton); } } </script> </head> <body> <form name='myform'> 要添加的个数:<input type="text" name="count"/> <br> <input type="button" value="添加" onclick="addButton()"> </form> </body></html> 那请问怎么做一个小的搜索,就是一个textform,输入一个四位数字,然后按一下button,显示两行文字,搜索内容从同一个div中表格中去配对,请教 1#、2#都不错2#还可以简写var newButton = document.createElement("input");newButton.type = "button";newButton.value = "button"+i;newButton.onclick = test;document.myform.appendChild(newButton); 顶!标签块插入,推荐使用innerHTML。 那请问,我如何管理buttom啊,比如说我有N个button,N个div ,创建出来后,我要点一个button,跟它对应的div就显示,再点一下div就隐藏,其他的button也是如此 那需要在button中添加事件处理,可以将div的id保存在button的属性中点击下判断div是否隐藏,如果隐藏则显示,反正就隐藏 div的id怎么保存到button的属性中啊? 不太懂 document.getElementById(divId).style.display = none//隐藏document.getElementById(divId).style.display = block//显示其中的divId是你要隐藏或者显示的div的页面id 动态创建的div 跟 button传过来的值总是最后一个啊,我需要钱面的也可以 隐藏 显示 有很多的button 跟div <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>c.html</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><script type="text/javascript">function add(id) { var divBox = document.createElement("div"); divBox.innerHTML = "<input type='button' value='btn"+id+"' id='btn"+id+"' onclick='test(this);'/>"; document.getElementById("show").appendChild(divBox); var divBox2= document.createElement("div"); divBox2.innerHTML = "<div id='div"+id+"' style='width:100px;height:100px;background:#000;display:none;'>'div"+id+"'</div>"; document.getElementById("show").appendChild(divBox2);}function test(obj){ var btnId=obj.id; var divId=btnId.replace("btn","div"); if(document.getElementById(divId).style.display=="none"){ document.getElementById(divId).style.display="block"; } else{ document.getElementById(divId).style.display="none"; }}function addElement(count){ for(var i=0;i<count;i++){ add(i); }}</script></head><body><input type="button" value="添加" onClick="addElement(6)"><div id="show"></div> </body></html> 这个<div id=article1 可以向上正常滚动,但是能不能再加一个<div id=article3 请教js脚本在不同浏览器中执行效率的问题 这段代码运行不了,请帮忙看看 求助:关于js问题 如何制作底部浮动图片??急哦。。 js 读文件 相对路径 请指点一下 onFocus onBlur 有何用法?举一些简单的实例 超高难度问题(请问哪位大侠怎么使ie的后退按钮失效即使IE的后退按钮被屏蔽)在线等 求助:用JSP实现统计来访人数,请问怎样实现? extjs5.0分页失败 怎么解决这个问题 json怎样实现动态添加
<html>
<head>
<title>c.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function add() {
var divBox = document.createElement("div");
divBox.innerHTML = "<input type='button' value='button' onclick='test();'/>";
document.getElementById("show").appendChild(divBox);
}
function test(){
alert("this is a test.");
}
</script>
</head>
<body>
<input type="button" value="添加" onClick="add()">
<div id="show"></div>
</body>
</html>
<html>
<head>
<script type="text/javascript">
var test = function(){
alert("我被创建了");
}
function addButton(){
var count = parseInt(document.myform.count.value);
for(var i = 0;i<count;i++){
var newButton = document.createElement("input");
newButton.setAttribute("type","button");
newButton.setAttribute("value","button"+i);
newButton.onclick = test;
document.myform.appendChild(newButton);
}
}
</script>
</head>
<body> <form name='myform'>
要添加的个数:<input type="text" name="count"/>
<br>
<input type="button" value="添加" onclick="addButton()">
</form>
</body>
</html>
2#还可以简写var newButton = document.createElement("input");
newButton.type = "button";
newButton.value = "button"+i;
newButton.onclick = test;
document.myform.appendChild(newButton);
顶!标签块插入,推荐使用innerHTML。
那请问,我如何管理buttom啊,比如说我有N个button,N个div ,创建出来后,我要点一个button,跟它对应的div就显示,再点一下div就隐藏,其他的button也是如此
那需要在button中添加事件处理,可以将div的id保存在button的属性中
点击下判断div是否隐藏,如果隐藏则显示,反正就隐藏
div的id怎么保存到button的属性中啊? 不太懂
document.getElementById(divId).style.display = block//显示
其中的divId是你要隐藏或者显示的div的页面id
动态创建的div 跟 button传过来的值总是最后一个啊,我需要钱面的也可以 隐藏 显示
有很多的button 跟div
<html>
<head>
<title>c.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function add(id) {
var divBox = document.createElement("div");
divBox.innerHTML = "<input type='button' value='btn"+id+"' id='btn"+id+"' onclick='test(this);'/>";
document.getElementById("show").appendChild(divBox);
var divBox2= document.createElement("div");
divBox2.innerHTML = "<div id='div"+id+"' style='width:100px;height:100px;background:#000;display:none;'>'div"+id+"'</div>";
document.getElementById("show").appendChild(divBox2);
}
function test(obj){
var btnId=obj.id;
var divId=btnId.replace("btn","div");
if(document.getElementById(divId).style.display=="none"){
document.getElementById(divId).style.display="block";
}
else{
document.getElementById(divId).style.display="none";
}
}
function addElement(count){
for(var i=0;i<count;i++){
add(i);
}
}
</script>
</head>
<body>
<input type="button" value="添加" onClick="addElement(6)">
<div id="show"></div>
</body>
</html>