拥有带方法的文字对象。
我想使用appendChild DOM方法将方法链接到彼此。 在createStructure方法中,如果我将赋值彼此分开,则可以工作。
var row = this.createRow();
row.appendChild(this.createHeadCells());
this.DBTable.appendChild(row);
如果我将方法链接到彼此,它不会,没有错误信息。
this.DBTable.appendChild(this.createRow().appendChild(this.createHeadCells()));
否则此链接正在起作用:
document.body.appendChild(document.createElement('p').appendChild(document.createElement('span').appendChild(document.createTextNode('abc')));
我错了什么?
守则:
var readyStateCheckInterval = setInterval(function () {
if (document.readyState === "complete" || document.readyState === "loaded") {
clearInterval(readyStateCheckInterval);
init();
}
}, 10);
var DBTable =
{
DBTable : document.createDocumentFragment(),
colNum : 0,
rowNum : 0,
createTable : function(colNum, rowNum)
{
this.DBTable = document.createElement('table');
this.colNum = colNum;
this.rowNum = rowNum;
this.createTableStructure();
},
createRow : function()
{
return document.createElement('tr');
},
createHeadCells : function()
{
var df = document.createDocumentFragment();
for ( var a = 0; a < this.colNum; a++)
{
df.appendChild(document.createElement('th'));
}
return df;
},
createDataCells : function()
{
var df = document.createDocumentFragment();
for ( var a = 0; a < this.colNum; a++)
{
df.appendChild(document.createElement('td'));
}
return df;
},
createTableStructure : function()
{
var row = this.createRow();
row.appendChild(this.createHeadCells());
this.DBTable.appendChild(row);
this.DBTable.appendChild(this.createRow().appendChild(this.createHeadCells()));
/*
for ( var a = 0; a < this.rowNum; a++) {
var row = this.createRow();
row.appendChild(this.createDataCells());
this.DBTable.appendChild(row);
//this.DBTable.appendChild(this.createRow().appendChild(this.createDataCells()));
}
*/
document.body.appendChild(this.DBTable);
}
};
function init() {
DBTable.createTable(3,4);
var td = document.querySelectorAll('td');
for (var el in td) {
td[el].innerHTML = 'asdf';
}
var th = document.querySelectorAll('th');
for (var el2 in th) {
th[el2].innerHTML = 'abcd';
}
}