<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
table{
font-size:24px;
font-style:italic;
}
.s1{
background-color:#ff99ee;
}
</style>
<script src="prototype-1.6.0.3.js"></script>
<script>
/*dom方式*/
function addRow(){
var name=$f("name");
var salary=$f("salary");
var td1=document.createElement("td");
var td2=document.createElement("td");
td1.innerHTML=name;
td2.innerHTML=salary;
var tr1=document.createElement("tr");
tr1.appendChild(td1);
tr1.appendChild(td2);
$("tb").appendChild(tr1);
}
</script>
</head>
<body style="font-size:30px;">
<table width="60%" border="1" cellpadding="0" cellspacing="0" id="tb">
<tr>
<td>姓名<br></td>
<td>薪水 <br></td></tr>
<tr>
<td>zs<br></td>
<td>2000 <br></td></tr>
<tr>
<td>ww <br></td>
<td>3000 <br></td></tr>
</table>
姓名:<input name="name" id="name"/>薪水:<input name="salary" id="salary"/><input type="button" value="添加" onclick="addRow();">
</body>
</html>
我的目的是要在文本框中输入内容,然后点添加按钮,就会自动增加一行,为什么没效果呢,求救啊
------解决方案--------------------
input 值的获取有些问题,试试
- JScript code
var name=$("name").value; var salary=$("salary").value;
------解决方案--------------------
- HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <style> table{ font-size:24px; font-style:italic; } .s1{ background-color:#ff99ee; } </style> <script src="prototype-1.6.0.3.js"></script> <script> /*dom方式*/ function addRow(){ var name=$f("name"); var salary=$f("salary"); var td1=document.createElement("td"); var td2=document.createElement("td"); td1.innerHTML=name; td2.innerHTML=salary; var tr1=document.createElement("tr"); tr1.appendChild(td1); tr1.appendChild(td2); [color=#FF0000]$("tb").append(tr1);[/color]//jquery 中是用append方法,JS中才用appendChild } </script> </head> <body style="font-size:30px;"> <table width="60%" border="1" cellpadding="0" cellspacing="0" id="tb"> <tr> <td>姓名<br></td> <td>薪水 <br></td></tr> <tr> <td>zs<br></td> <td>2000 <br></td></tr> <tr> <td>ww <br></td> <td>3000 <br></td></tr> </table> 姓名:<input name="name" id="name"/>薪水:<input name="salary" id="salary"/><input type="button" value="添加" onclick="addRow();"> </body> </html>
------解决方案--------------------
- JScript code
function addRow(){ //获取值 var name=$("#name").val(); var salary=$("#salary").val(); //创建新增的结构 var tr1=document.createElement("tr"); var td1=document.createElement("td"); var td2=document.createElement("td"); tr1.appendChild(td1); tr1.appendChild(td2); td1.innerHTML=name; td2.innerHTML=salary; //追加行 $("#tb").append(tr1); }