疯了,setAttribute(key,value)方法的问题。急

jianjialin 2009-09-03 01:04:38

var tbody = document.getElementById('tb_paymentplan');


var tr = document.createElement('tr');tr.setAttribute('id', 'tr_' + rowIndex);

//第一列 索引
var td1 = document.createElement('td'); td1.setAttribute('id', 'id_index' + rowIndex);
td1.appendChild(document.createTextNode(rowIndex.toString()));
tr.appendChild(td1);

//第二列
var td2 = document.createElement('td');
var txtprojectName = document.createElement('input');
txtprojectName.setAttribute('type', 'text'); txtprojectName.setAttribute('id', 'txt_add_PaymentPlanName' + rowIndex);
td2.appendChild(txtprojectName);
tr.appendChild(td2);


//第三列
var td3 = document.createElement('td');
var txtMoneyInPlan = document.createElement('input');
txtMoneyInPlan.setAttribute('type', 'text'); txtMoneyInPlan.setAttribute('id', 'txt_add_MoneyInPlan' + rowIndex);
txtMoneyInPlan.setAttribute('onkeypress', 'return noNumbers(this,event,true)');txtMoneyInPlan.setAttribute('onkeyup', 'CompleteTotalMoney()');txtMoneyInPlan.setAttribute('onpaste', 'return false');
td3.appendChild(txtMoneyInPlan);
tr.appendChild(td3);


var td4 = document.createElement('td');
var txtDateInPlan = document.createElement('input');
txtDateInPlan.setAttribute('type', 'text'); txtDateInPlan.setAttribute('id', 'txt_add_DateInPlan' + rowIndex); txtDateInPlan.setAttribute('onclick', 'setday(this)');
txtDateInPlan.setAttribute('readonly','readonly');
td4.appendChild(txtDateInPlan);
tr.appendChild(td4);

var td_isawoke = document.createElement('td');
var ckbIsawoke = document.createElement('input');
ckbIsawoke.setAttribute('type', 'checkbox'); ckbIsawoke.setAttribute('id', 'ckb_add_IsAwoke' + rowIndex); ckbIsawoke.setAttribute('value', '1');
td_isawoke.appendChild(ckbIsawoke);
tr.appendChild(td_isawoke);

var td5 = document.createElement('td');
var btnDelete = document.createElement('input');
btnDelete.setAttribute('type', 'button'); btnDelete.setAttribute('id', 'btn_add_Delete' + rowIndex); btnDelete.setAttribute('value', '删除'); btnDelete.setAttribute('onclick', 'deleteTR(' + rowIndex + ')');
td5.appendChild(btnDelete);
tr.appendChild(td5);

rowIndex = rowIndex + 1;
tbody.appendChild(tr);
...全文
183 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yixianggao 2009-09-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hookee 的回复:]
是 btnDelete.setAttribute('type', 'button'); 不起作用?代码测试下来可以显示按钮的
如果是定义事件的话,用
btnDelete.onclick = function(){}
[/Quote]
确实,IE 7 下没问题滴,估计 IE 8 应该也可以,
如果是 IE 6 就要用 btnDelete.type = 'button';
jianjialin 2009-09-03
  • 打赏
  • 举报
回复
[Code=JScript]
var sb = "<TR id=tr_"+rowIndex+">"+
"<TD id=id_index" + rowIndex + ">" + rowIndex + "</TD>" +
"<TD><INPUT id=txt_add_PaymentPlanName" + rowIndex + " type=text></TD>" +
"<TD><INPUT id=txt_add_MoneyInPlan" + rowIndex + " onkeypress=\"return noNumbers(this,event,true)\" onkeyup=CompleteTotalMoney() onpaste=\"return false\" type=text></TD>" +
"<TD><INPUT id=txt_add_DateInPlan" + rowIndex + " onclick=setday(this) readOnly type=text></TD>" +
"<TD><INPUT id=ckb_add_IsAwoke1 value=" + rowIndex + " type=checkbox></TD>" +
"<TD><INPUT id=btn_add_Delete"+rowIndex+" onclick=deleteTR("+rowIndex+") value=删除 type=button /></td></tr>";
$("#tb_paymentplan").append(sb);
[/Code]

执行这样一段代码,再alert($("#tb_paymentplan").html());弹出来的代码有些input元素居然还是没有type。。。
孟子E章 2009-09-03
  • 打赏
  • 举报
回复
<body>
<script>
var txtprojectName = document.createElement('input');
txtprojectName.type="button"
document.body.appendChild(txtprojectName)
</script>
yixianggao 2009-09-03
  • 打赏
  • 举报
回复
You must perform a second step when using createElement to create the input element. The createElement method generates an input text box, because that is the default input type property. To insert any other kind of input element, first invoke createElement for input, then set the type property to the appropriate value in the next line of code.
——DHTML参考手册

L@_@K
<!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>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>

<body>
<script type="text/javascript">
<!--
var btnDelete = document.createElement('input');
btnDelete.type = 'button';
btnDelete.setAttribute('id', 'btn_add_Delete');
btnDelete.setAttribute('value', '删除');
btnDelete.onclick = function() {
alert(this.id);
};
document.body.appendChild(btnDelete);
//-->
</script>
</body>
</html>


Web 开发常用手册

JScript语言参考.rar
http://download.csdn.net/source/308916

DHTML参考手册.rar
http://download.csdn.net/source/308913

样式表中文手册.chm
http://download.csdn.net/source/304124
hookee 2009-09-03
  • 打赏
  • 举报
回复
是 btnDelete.setAttribute('type', 'button'); 不起作用?代码测试下来可以显示按钮的
如果是定义事件的话,用
btnDelete.onclick = function(){}
BeenZ 2009-09-03
  • 打赏
  • 举报
回复

IE下要用 document.getElementById("ID").TYPE=VALUE;
FF下可以用 document.getElementById("ID").setAttribute(TYPE,VALUE)
jianjialin 2009-09-03
  • 打赏
  • 举报
回复
日 点急了。

这段代码造出来的input 元素 始终没有 text属性!

也就是说

btnDelete.setAttribute('type', 'button');

类似这种代码始终没有作用。为什么

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧