initialize()函数内部有一个jQuery each循环.在该循环内部的引用this.dbcolumns显然不起作用,因为jQuery已重新分配this给当前循环元素.那么如何this.dbcolumns从循环内部引用呢?它在循环外工作正常.
function datatable() {
this.url = '';
this.htmltable = '';
this.dbtable = '';
this.dbcolumns = new Array();
this.idfield = 'id';
this.pageno = 0;
this.pagesize = 15;
this.totalpages = 0;
this.totalrecords = 0;
this.searchterm = '';
this.initialize = function() {
this.dbtable = $(this.htmltable).attr('data-table');
this.dbcolumns.push(this.idfield);
$(this.htmltable + ' th[data-field]').each(function(i, col){
this.dbcolumns.push( $(col).attr('data-field') ); /* <<<<<<<<<< this line */
});
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
Cal*_*leb 14
引用要保留在循环外部的"this".
var self = this;
Run Code Online (Sandbox Code Playgroud)
然后你可以在循环中使用"self".
| 归档时间: |
|
| 查看次数: |
6816 次 |
| 最近记录: |