如何更改为<input id =“totalCell”/>

时间:2014-03-27 04:43:07

标签: javascript

任何人都可以帮我改变td id =&#34; totalCell&#34;输入id =&#34; totalCell&#34;因为我想在输入中添加onblurr但它无法正常工作

<script type="text/javascript">
function updateTotal(s){
var newTotal=0;
var allEls=document.getElementsByTagName("*");
for(var i=0;i<allEls.length;i++)
{
if(allEls[i].getAttribute("name")!=s)continue;
if(isNaN(allEls[i].value))continue;
newTotal+=new Number(allEls[i].value);
}
var e=document.getElementById("totalCell");
e.firstChild.data="  Total =  "+newTotal;
} 
</script>
<html>
<body>
<table><tr>
<td><input type="text" onblur="updateTotal('putts[]')" size="2" name="putts[]"         maxlength="1" value=""/></td>
<td><input type="text" onblur="updateTotal('putts[]')" size="2" name="putts[]" maxlength="1" value=""/></td>
<td><input type="text" onblur="updateTotal('putts[]')" size="2" name="putts[]" maxlength="1" value=""/></td>
<td id="totalCell">&nbsp;</td>
</tr></table>

1 个答案:

答案 0 :(得分:0)

如果您希望将<td id=“totalCell”>替换为<td><input id=“totalCell”>,请执行

//selecting the existing dom element to be replaced
var OldElement =document.getElementById("totalCell");

//building the new element to replace old 
var NewTd = document.createElement("td");
var myInput = document.createElement("input");
myInput.setAttribute("id", "totalCell");
NewTd.appendChild(myInput);

//replace old by new
OldElement.parentNode.replaceChild(NewTd, OldElement);

请参阅此working fiddle