这是我正在处理的一些 HTML 页面的 Javascript 代码:
var widgetsList = $("[widgetId]");
ToLogger("Init: " + widgetsList.length + " widgets found.");
for (var i=0; i<widgetsList.length; i++)
{
var widget = widgetsList[i];
var jqID = "#" + widget.id;
var refreshInterval = $(jqID).attr("refreshInterval");
if (refreshInterval == undefined)
refreshInterval = "30";
$(jqID).data("refreshCounter", "0");
var x = $(jqID).data("refreshCounter");
ToLogger(" = widget[ " + widget.id + " ].refreshCounter(init1) = " + x);
var widgetObj = $(jqID);
jQuery.data(widgetObj, "refreshCounter", "0");
x = jQuery.data(widgetObj, "refreshCounter");
ToLogger(" = widget[ " + widget.id + " ].refreshCounter(init2) = " + x);
}
但在日志输出中我看到:
DashboardInit: 1 widgets found.
= widget[ Q.RecordCounters ].refreshInterval = 30
= widget[ Q.RecordCounters ].refreshCounter(init1) = undefined
= widget[ Q.RecordCounters ].refreshCounter(init2) = 0
为什么 '$(jqID).data("refreshCounter")' 返回 'undefined'?一个月前,这个 javascript 代码运行良好!现在它有什么问题?我已经使用 jQuery 1.7.2 和 1.8.1 进行了尝试——工作方式相同。难道是因为浏览器更新了?(火狐15.0)
下一个问题 - 甚至 jQuery.data(...) 在计时器函数中也不起作用:
var timerLock = false;
function OnTimer()
{
if (timerLock) return ;
timerLock = true;
for (var i=0; i<widgetsList.length; i++)
{
var widget = widgetsList[i];
var jqID = "#" + widget.id;
var widgetObj = $(jqID);
var x = jQuery.data(widgetObj, "refreshCounter");
var counter = parseInt(x);
ToLogger(" = widget[ " + widget.id + " ].refreshCounter = " + x + " / " + counter);
}
}
但在日志输出中我看到:
= widget[ Q.RecordCounters ].refreshCounter = undefined / NaN
为什么是“未定义”?!如果 .data("refreshCounter") 之前已成功分配,那么我认为它一定不是“未定义”。你看到了吗 - 它可能有什么问题?