在绘制数据驱动网格的页面底部附近,网格包具有此代码......
<script type="text/javascript">
(function(window, document, undefined) {
"use strict";
var gridColSortTypes =
["string", "string", "number", "number", "number", "number", "number", "number", "number", "number"],
gridColAlign = [];
var onColumnSort = function( newIndexOrder, columnIndex, lastColumnIndex ) {
var doc = document;
var offset = (this.options.allowSelections && this.options.showSelectionColumn)?1:0;
if( columnIndex !== lastColumnIndex ) {
if( lastColumnIndex > -1 ) {
doc.getElementById("stockHdr" + (lastColumnIndex-offset)).parentNode.style.backgroundColor = "";
}
doc.getElementById("stockHdr" + (columnIndex-offset)).parentNode.style.backgroundColor = "#f7f7f7";
}
};
...
})(this, this.document);
调查指定排序列颜色更改失败的原因,我发现尽管getElementById()查询的对象存在且在页面中正常运行,但在此函数内,这些getElementById()调用返回Null。实际上,从此函数调用的所有getElementById()调用都返回Null。
为什么?
答案 0 :(得分:1)
当您尝试访问DOM内容时,我会假设DOM内容尚未就绪/可访问,请尝试将代码包装在window.onload
中:
window.onload = function(){
(function(window, doc, undefined) {
"use strict";
var gridColSortTypes =
["string", "string", "number", "number", "number", "number", "number", "number", "number", "number"],
gridColAlign = [];
var onColumnSort = function( newIndexOrder, columnIndex, lastColumnIndex ) {
var offset = (this.options.allowSelections && this.options.showSelectionColumn)?1:0;
if( columnIndex !== lastColumnIndex ) {
if( lastColumnIndex > -1 ) {
doc.getElementById("stockHdr" + (lastColumnIndex-offset)).parentNode.style.backgroundColor = "";
}
doc.getElementById("stockHdr" + (columnIndex-offset)).parentNode.style.backgroundColor = "#f7f7f7";
}
};
...
})(this, this.document);
};