任何人都可以帮我改变td id =" totalCell"输入id =" totalCell"因为我想在输入中添加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"> </td>
</tr></table>
答案 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