a

本文介绍了一个使用HTML和JavaScript实现的动态表格案例。该案例通过JavaScript实现了对HTML表格的增删改操作,包括添加行、删除指定行以及选择项变化时更新对应输入框的功能。文章详细展示了如何创建表格元素、添加选项并设置事件监听来改变表格内容。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Test</title>
</head>

<body>
<script>

var textNumber = 1;

function change(a)
{
/*两个都不行*/
//document.all.box.value = document.all.select1.value;
//document.all.input1.value = document.all.oSelect.value;
alert("123")
//alert(a.value)
//document.getElementById("box").value=document.getElementById("selec").value
//alert(a.parentNode.parentNode.innerHTML)
//a.parentNode.parentNode.parentNode.childNodes[1].childNodes[0].value=a.value
//alert(a.parentNode.parentNode.childNodes[1].value=a.value)
a.parentNode.nextSibling.childNodes[0].value=a.value
}

function DeleteTr(i)
{
var table = document.getElementById("table1");
var tr = table.childNodes[0];

tr.removeChild(tr.childNodes[i]);
}

function AppendTr()
{
textNumber++;

var table = document.getElementById("table1");
var tr = table.childNodes[0];

var trnew = document.createElement("tr");

var td1 = document.createElement("td");

var oSelect = document.createElement("select");
  oSelect.setAttribute("id","select1");
  oSelect.setAttribute("onChange","change()");
var option11 = document.createElement("option");
  option11.text="A.你";
  option11.value="YOU";
  oSelect.add(option11);
var option12 = document.createElement("option");
  option12.text="B.我";
  option12.value="MY";
  oSelect.add(option12);
var option13 = document.createElement("option");
  option13.text="C.他";
  option13.value="HE";
  oSelect.add(option13);

var td2 = document.createElement("td");
 
var input1 = document.createElement("input");
  input1.setAttribute("name","box");
  input1.setAttribute("id","box" );
 
//td1.appendChild(oSelect);
td1.innerHTML="<select onchange='change(this)' id='selec'><option value='YOU'> 你 </option><option value='YOU1'> 你1 </option></select>"
td2.appendChild(input1);

trnew.appendChild(td1);
trnew.appendChild(td2);

tr.appendChild(trnew);
}

</script>

<table  border="0" cellspacing="0"  bgcolor="#CCCCCC" id="table1">
</table>

<input type="button" onclick="DeleteTr(0);" value="删除一行">
<input type="button" onclick="AppendTr();" value="插入一行">
</body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值