补分贴:(300分求助:JS中——动态声明事件处理函数)含源贴内容

蓝海 2010-12-04 05:11:47
大家在这里回贴也一样,期待您的回贴。。。。。。

以下只是部分代码,源文件太大了!下面关键部分临时加了注释!

<script type="text/javascript">

// 创建 JSONSTORE
var menuStore =new Ext.data.JsonStore({
url : 'loadRoleMenus.do',
root:'records',
fields:['code','name','jspurl','isLeaf','parentcode'],
autoLoad : true
});

// 加载数据
menuStore.on('load', function(_store, _records, _options){
for (var i = 0; i < _records.length; i++) {
var parentcode= _records[i].get('parentcode');
if (Ext.isEmpty(parentcode)){
addRootMenu(_records[i]);
}else{
continue;
}
}
});

// 添加根菜单
function addRootMenu(node) {
//Get XmlString
var sXmlString = "";
var sBegin = "<root><menu><ul>";
var sEnd = "</ul></menu></root>";

sXmlString = sBegin + getChildXmlString(node.data.code) + sEnd;
addMainMenu(node.data.name, sXmlString); // 添加根菜单
}

// 获取单个根菜单下的所有子菜单栏的 XML 字符串
function getChildXmlString(strParent) {
var sMenuItem = ""
var sLeft = "<li onclick=";
sLeft += '"';
sLeft += "{fnCallJspFuncPage('";
var sCenter = "')}";
sCenter += '">';

var sRight = "</li>";
for (var i = 0; i < menuStore.getCount(); i++) {
if (menuStore.getAt(i).data.parentcode==strParent) {
sMenuItem = sMenuItem + sLeft + menuStore.getAt(i).data.jspurl + sCenter + menuStore.getAt(i).data.name + sRight;
}
}
return sMenuItem;
}
// welcome.js end

// 将来想扩展成动态的数组对象,目前数组固定为 6 个元素
var arrMenuMain = new Array(6);
var intMenuIndx = 1;

arrMenuMain[0] = new Menu();
arrMenuMain[0].id = "MenuA";
arrMenuMain[1] = new Menu();
arrMenuMain[1].id = "MenuB";
arrMenuMain[2] = new Menu();
arrMenuMain[2].id = "MenuC";
arrMenuMain[3] = new Menu();
arrMenuMain[3].id = "MenuD";
arrMenuMain[4] = new Menu();
arrMenuMain[4].id = "MenuE";
arrMenuMain[5] = new Menu();
arrMenuMain[5].id = "MenuF";

// 添加根菜单的处理函数
function addMainMenu(strMenuItemText, strXmlString){
var tdMnu = document.getElementById("rowMenuMain").insertCell();
var sID = "Menu" + String.fromCharCode(intMenuIndx+64);
tdMnu.Id = "TD-" + sID;
// 产生 LI对象 及其属性串
var sRes = '<li id="';
sRes += sID;
sRes += '" ';
sRes += 'onmouseover="{this.className = ';
sRes += "'over'}";
sRes += '" onmouseout=';
sRes += '"{this.className = ';
sRes += "'out'}";
sRes += '"';
sRes += " class='out'>";
sRes += strMenuItemText + "</li>";
tdMnu.innerHTML = sRes;
tdMnu.width = 100;
tdMnu.align = "center";
arrMenuMain[intMenuIndx-1].width = 180;
arrMenuMain[intMenuIndx-1].xmlString = strXmlString;
arrMenuMain[intMenuIndx-1].init();
intMenuIndx += 1;
}


// 问题就出在下面的代码:BEGIN
document.getElementById("MenuA").onmousemove = function(e) {
for (var i = 0; i <= 5; i++) {
var sItm = "Menu" + String.fromCharCode(i+65);
if (sItm=="MenuA") {
arrMenuMain[i].show(e, "left");
} else {
arrMenuMain[i].close(e);
}
}
}

// MenuB、MenuC、MenuD、MenuE、MenuF 与 MenuA 中代码相同,此处略掉

// 问题就出在下面的代码:END

// IE 中的提示是这样的“document.getElementById("...")为空,或不是对象”

</script>

</body>
</html>
...全文
72 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mr-Jee 2010-12-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 msailsoft 的回复:]

问题已经解决:

原因是 JSONSTORE 加载数据的方式是异步的。

也就是说,在 JSONSTORE 还没加载(LOAD)完数据以前,下面的 JS 代码已经执行完毕了,

,当下面的代码执行到 document.getElementById("MenuA").onmousemove 时 ID 为 MenuA

的对象还没有来得及创建!


求助贴改为散分贴了,大家接……
[/Quote]看来这个问题很多童鞋都没有深刻的印象啊~~有必要整理点内容出来了~
cvpc 2010-12-04
  • 打赏
  • 举报
回复
异步操作要记得判断状态。jf来了
flowerjack 2010-12-04
  • 打赏
  • 举报
回复
继续接分
as123456789d 2010-12-04
  • 打赏
  • 举报
回复
接分了.....
蓝海 2010-12-04
  • 打赏
  • 举报
回复
问题已经解决:

原因是 JSONSTORE 加载数据的方式是异步的。

也就是说,在 JSONSTORE 还没加载(LOAD)完数据以前,下面的 JS 代码已经执行完毕了,

,当下面的代码执行到 document.getElementById("MenuA").onmousemove 时 ID 为 MenuA

的对象还没有来得及创建!


求助贴改为散分贴了,大家接分吧。。。。。。。。。。
hch126163 2010-12-04
  • 打赏
  • 举报
回复
arrMenuMain[0] = new Menu();
arrMenuMain[0].id = "MenuA";


你的 Menu 是什么对象啊?是一个dom 对象吗?
你用firebug 看看它创建的html对象有 id= "MenuA"; 的 dom 对象吗?

如果arrMenuMain[0] = new Menu(); 是一个dom 对象

可以:

arrMenuMain[0].onmousemove = function(e) {
for (var i = 0; i <= 5; i++) {
var sItm = "Menu" + String.fromCharCode(i+65);
if (sItm=="MenuA") {
arrMenuMain[i].show(e, "left");
} else {
arrMenuMain[i].close(e);
}
}
}
kkbac 2010-12-04
  • 打赏
  • 举报
回复
用chrome浏览器调试下, 看看具体错误是哪里?

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧