<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/javascript">
window.onload=function() {
var btnAdd = document.getElementById('btnAdd');
var tempTab = document.getElementById('tempTab');
btnAdd.onclick = function() {
var oTr = document.createElement('tr');
var oTd1 = document.createElement('td');
oTd1.innerHTML = tempTab.tBodies[0].rows.length+1;
oTr.appendChild(oTd1);
var oTd2 = document.createElement('td');
oTd2.innerHTML = tempTab.tBodies[0].rows.length+1;
oTr.appendChild(oTd2);
var oTd3 = document.createElement('td');
var oInput = document.createElement('input');
oInput.type = 'button';
oInput.value = '点我';
oInput.onclick = function () {
alert('第'+this.parentNode.parentNode.rowIndex+'行,第'+(this.parentNode.cellIndex+1)+'列');
};
oTd3.appendChild(oInput);
oTr.appendChild(oTd3);
tempTab.tBodies[0].appendChild(oTr);
};
}
</script>
</head>
<body>
<input id="btnAdd" type="button" value="点我向表格里面添加input按钮"/>
<table id="tempTab" border="1" width="300">
<thead>
<tr>
<td>第一列</td>
<td>第二列</td>
<td>第三列</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
function CreateTextBox(element, value) {
//检查编辑状态,如果已经是编辑状态,跳过
var editState = element.getAttribute("EditState");
if (editState != "true") {
//创建文本框
var textBox = document.createElement("INPUT");
textBox.type = "text";
textBox.className = "EditCell_TextBox";
textBox.setAttribute("onkeyup", "if(isNaN(value))execCommand('undo')");
textBox.setAttribute("onafterpaste", "if(isNaN(value))execCommand('undo')")
textBox.setAttribute("style", "ime-mode:disabled");
textBox.setAttribute("onkeydown","if(event.keyCode==32) return false"); //设置文本框当前值
if (!value) {
value = element.getAttribute("Value");
}
textBox.value = value;
}就这个代码。
textbox.parentNode.cellIndex+1获取不了。