【发布时间】:2014-02-01 12:52:13
【问题描述】:
我有两个页面,都具有相似的结构 - 带有如下 id 标签的表格
<TABLE id="assetsTable1" width="100%" class="tableWrapDataClass"> ...
和
<table id="vocationalRevenueTable" class="revenue" width="100%"> ...
两个页面都使用几乎相同的 JavaScript 代码。但是全局变量的值在第 2 页上的行为不同!
这是第 1 页的 sn-p;
<script type="text/javascript" src="templates/footer.js"></script>
<script type="text/javascript" src="script/jquery-1.8.1.js"></script>
<script type="text/javascript" src="script/scripts.js"></script>
<script>
$(document).ready(function() {
var assetsTable1 = $('#assetsTable1');
var assetsTable2 = $('#assetsTable2');
var liabilitiesTable1 = $('#liabilitiesTable1');
var liabilitiesTable2 = $('#liabilitiesTable2');
var shareholderTable = $('#shareholderTable');
// note not all subtotals are shown
$("#A2Row1").hide();
$("#A2Row2").hide();
$("#L2Row1").hide();
$("#L2Row2").hide();
console.log(window.assetsTable1);
});
// etc
从第 2 页开始,
<script type="text/javascript" src="templates/footer.js"></script>
<script type="text/javascript" src="script/jquery-1.8.1.js"></script>
<script type="text/javascript" src="script/scripts.js"></script>
<script>
$(document).ready(function() {
var vocRevTableRef = $('#vocationalRevenueTable');
var nonVocRevTableRef = $('#nonVocationalRevenueTable');
var otherRevTableRef = $('#otherRevenueTable');
// note not all subtotals are shown initially
$("#vocationalRevenueTableSub").hide();
$("#nonVocationalRevenueTableSub").hide();
$("#otherRevenueTableSub").hide();
console.log(window.vocRevTableRef);
});
// etc
在 Firebug 中,第 1 页上的 console.log() 调用显示得很合理
<table id="assetsTable1" class="tableWrapDataClass" width="100%">
然而,在第 2 页,它返回
undefined
但是,如果我修改两个页面上的 console.log() 调用以删除“窗口”。位,例如;
console.log(assetsTable1);
和
console.log(vocRevTableRef);
一切恢复正常。在这两种情况下,Firebug 都会显示对象
Object[table#vocRevTableRef.tableWrapDataClass]
和
Object[table#assetsTable1.tableWrapDataClass]
window 对象出了点问题。当然,这完全是令人抓狂的。我可能会错过什么?我应该寻找什么来调试这个? (HTML 似乎没有格式错误。)我只是试图将有问题的对象引用传递到一个函数中,该函数从 thePassedReference.tbodies 中提取行并将它们求和,但当然它在第 2 页上失败了,因为它很神秘传递一个未定义的值。哈!谢谢。
【问题讨论】:
标签: javascript jquery global-variables window-object