使用obj.appendChild(newRow);在页面中添加一行代码(<tr><td><input id="sId">..</td>.....</tr>),此行代码中包括一个onclick事件,参数包括document.getElementById("sId")。现象是代码添加成功,点击执行onclick事件时,找不到sId这个对象,document.getElementById("sId")=null。查看源码,添加的此行代码的没有问题。费解ing
调试欢乐多
<body>
<div id="authorize">
<table>
<tbody id="eM0">
<tr id="e0_M" style="display:none;" class="lw">
<td class="liwai" name="aData">
<input name="dataNamee0_0" readonly="readonly" title="名称"/>
<img type="button" title="" style="cursor: hand;" src="query.gif"
onClick="CommonOnClickCOMMON_DATA_1(document.getElementById('dataIde0_0'),document.getElementById('dataNamee0_0'))" />
<input type="hidden" value='' name="dataIde0_0" />
<script type="text/javascript">
function CommonOnClickCOMMON_DATA_1(oId,oName){
var result = new Object();
result.name;
result.id;
var url="XXXXXXXXXXXXXXXXXXXXX";
var state="dialogWidth:600px;dialogHeight:450px;status:'no'";
result = showModalDialog(url,window,state);
if(result != null){
oId.value = result.id;
oName.value = result.name;
}
}
var resultA = [];
function setResult(value){
resultA[resultA.length] = value;
return resultA;
}
function getResult(){return resultA;}
function deleteResult(id){
for(var i=resultA.length;i>0;i--){
if(resultA[i-1][0]==id){
resultA.splice(i-1,1);}
}
}
function cleanResult(){
resultA = [];
}
</script>
</td>
</tr>
</tbody>
</table>
<div style="width:100%;margin:10px 17px 10px 4px;padding:10px 10px 10px 10px;border:#b7afdc 1px solid;" name="entity" id="151">
<table width="672" border="0" cellspacing="0" cellpadding="0" id="tba1">
<tbody id="o0">
</tbody>
<tbody id="e0" class="lw">
</tbody>
</table>
</div>
</div>
</body>
<script type="text/javascript">
function js1_callpage(htmlurl) {
var l=window.screen.width ;
var w= window.screen.height;
var al=l/2-200;
var aw=w/2-230;
var newwin=window.open(htmlurl,"homeWin","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=Auto,resizable=no,width=400,height=460,top="+aw+",left="+al+"");
newwin.focus();
return false;
}
function addRow(tbodyName,muban,mubanlw){
// Get the tbody
var orgBody = document.getElementById(tbodyName);
var copybody = document.getElementById(muban);
var copybodylw = document.getElementById(mubanlw);
var firstRow = copybody.rows[0];
var firstRowlw = copybodylw.rows[0];
if(orgBody.className == "lw")
{
// Call function to general the new ID
var newRow = firstRowlw.cloneNode(true);
var newID = shiftID(firstRowlw.id, orgBody.rows.length )
}
else{
// Call function to general the new ID
var newRow = firstRow.cloneNode(true);
var newID = shiftID(firstRow.id, orgBody.rows.length )
}
newRow.style.display = "block";
// Set the row's ID
newRow.id = newID;
// Reset the item status
resetRow(newRow);
// Reset the item number for this page only
resetItemNum(orgBody);
// add the new row
orgBody.appendChild(newRow);
alert("add row finish")
} function resetRow(obj){
//debugger;
var items = obj.childNodes;
//alert(items.length);
for (var m = 0; m < items.length; m ++){
// loop the elements of object
if (items[m].childNodes.length > 0){
resetRow(items[m]);
}
if (items[m] != undefined && items[m].type != undefined){
if (items[m].type == "select-one"){
items[m].selectIndex = -1;
items[m].value="";
}else if (items[m].type == "radio"){
// pass
//items[m].id = shiftID(items[m].id, newIndex);
}else if (items[m].type == "text"){
// Empty input text
items[m].value = "";
var ss = obj.parentNode.id;
var sd = ss.split("_");
var st = sd[1];
items[m].name = 'dataName'+ss;
}else if (items[m].type == "hidden"){
// Empty input text
items[m].value = "";
items[m].name = 'dataId'+ss;
}else if (items[m].type == "checkbox"){
// Empty input text
items[m].checked = false;
}else if (items[m].type == "button"){
// Empty input text
//debugger;
//items[m].onClick = "CommonOnClick(document.getElementById('dataId"+ss+"'),document.getElementById('dataName"+ss+"'))";
items[m].onclick = function(){
s = "alert(document.getElementById('dataId"+ss+"').outerHTML);CommonOnClick"+"COMMON_DATA_1"+"(document.getElementById('dataId"+ss+"'),document.getElementById('dataName"+ss+"'))"
eval(s);
};
}else{
break;
}
}
}
} function resetItemNum(tbody){
var item
for (var i = 1; i < tbody.rows.length; i ++){
}
} // General the new ID
function shiftID(oldID, rowNum){
var newNum = "" +rowNum;
// Split the last two characters
newNum = newNum.slice(newNum.length - 2, newNum.length)
return oldID.slice(0, oldID.length - 2) + "_" + newNum;
} // Delete the selected row
function deleteRow(tbodyName){
var tbody = document.getElementById(tbodyName);
if (tbody == null){
return;
}
// loop the rows
for (var i = tbody.rows.length -1; i >= 0 ; i --){
// selected row
if (rowSelected(tbody.rows[i])){
// delete the selected row if it has more than one row
if (tbody.rows.length > 0){
// remove the selected row
tbody.removeChild(tbody.rows[i]);
}else{
// reset the last row
resetRow(tbody.rows[i]);
}
}
}
// reset the item number
resetItemNum(tbody);
}
// check the special row is selected
function rowSelected(row){
var firstCell = row.cells[2];
var child = null;
for (var i = 0; i < firstCell.childNodes.length; i ++){
child = firstCell.childNodes[i];
if (child.type != undefined && child.type.toString() == "checkbox"){
return firstCell.childNodes[i].checked;
}
}
return false;
}
function test(rowID, textId){
var obj;
obj = document.getElementById(rowID);
var firstCell = obj.cells[0];
for (var i = 0; i < obj.cells.length; i ++){
for(var m= 0; m < obj.cells[i].childNodes.length; m ++){
var cell = obj.cells[i].childNodes[m];
//alert(cell.name);
if (cell.name==textId){
alert(cell.value);
}
}
}
}
</script>
opener.document.getElementById("txtname").value=somevalue;
function TestFn1() { var div = document.createElement("div");
div.id = "divvv1";
div.style.height = 20;
div.style.width = 20;
div.style.background = "red"; document.body.appendChild(div);
}function TestFn2() { document.getElementById("divvv1").innerHTML = "aaaaaaaaaaaaaaaa";}
方法一动态创建,方法二getElementById,我试了试,貌似我这边没有错,以前我也遇到过跟你一样的问题纳闷ing
[div#divvv1]然后就可以在上面加东西了:$('#divvv1').click(function(){
//blah blah
});