Jquery 上下文菜单
我有一个页面,其中有两种类型的 div(路由器和交换机),按属性“类型”进行区分。我希望右键单击它们时有不同的菜单。所以我所做的是使用 jquery 选择器获取所有类型为 router 的 div,并迭代它们以绑定上下文菜单。我面临的问题是,当我右键单击一个菜单项时,该菜单项将调用该 div id 的 jquery 模式对话框,现在如果我在另一个 div 上调用相同的菜单,也会为第一个 div id 调用该对话框。代码:
function createMenu() {
var divList = $("div[type^='Router']");
for ( var i = 0; i < divList.length; i++) {
// alert(divList[i].id);
$("#" + divList[i].id).contextMenu({
menu : 'routerMenu'
}, function(action, el, pos) {
var name = $(el).attr('name');
if(action == 'dialog') {
alert(name);
invokeDialog(name,'Router');
}
});
}
var divList = $("div[type^='Switch']");
for ( var i = 0; i < divList.length; i++) {
// alert(divList[i].id);
$("#" + divList[i].id).contextMenu({
menu : 'switchMenu'
}, function(action, el, pos) {
var name = $(el).attr('name');
if(action == 'dialog') {
alert(name);
invokeDialog(name,'Switch');
}
});
}
}
例如,有两个相同类型“Router”的 div,其 id 为 id1 和 id2。首先,我单击 id1,我收到一条带有 id1 名称的警报。接下来,我单击 id2,收到警报 id1,然后收到 id2。有谁知道我做错了什么?
I have a page where there are divs which are of 2 types (router & switch) differentiated by attribute 'type'. I want to have different menus when I right click them. So what I did is get all divs with type router using jquery selector and iterate over them to bind the context menu. The issue which I face is when I right click on a menu item which in turn will invoke a jquery modal dialog for that div id, now if I invoke the same menu on another div the dialog is invoked for the first div id also. The code :
function createMenu() {
var divList = $("div[type^='Router']");
for ( var i = 0; i < divList.length; i++) {
// alert(divList[i].id);
$("#" + divList[i].id).contextMenu({
menu : 'routerMenu'
}, function(action, el, pos) {
var name = $(el).attr('name');
if(action == 'dialog') {
alert(name);
invokeDialog(name,'Router');
}
});
}
var divList = $("div[type^='Switch']");
for ( var i = 0; i < divList.length; i++) {
// alert(divList[i].id);
$("#" + divList[i].id).contextMenu({
menu : 'switchMenu'
}, function(action, el, pos) {
var name = $(el).attr('name');
if(action == 'dialog') {
alert(name);
invokeDialog(name,'Switch');
}
});
}
}
For example, there are two divs of the same type 'Router' with ids id1 and id2. First I click on id1, I get an alert with id1's name. Next I click on id2, I get an alert id1 and then id2. Does anyone know what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论