我正在使用dijit中的上下文菜单。代码几乎完成,当我右键单击附加的dom节点时弹出菜单。 问题是我需要能够以编程方式显示/隐藏菜单。似乎没有像.show()或.hide()这样的方法
这是代码:
var top_settings_menu;
require(["dojo/ready", "dijit/Menu"], function(ready, Menu) {
ready(function() {
top_settings_menu = new Menu();
top_settings_menu.bindDomNode("settings");
...menu items
........
top_settings_menu.startup();
});
});
这是我需要的: 显示方法以编程方式隐藏菜单: top_settings_menu.show(); top_settings_menu.hide();
答案 0 :(得分:1)
This fiddle演示了两种方法,但我不知道是否有正式方法可以做到这一点。
设置菜单:
var pMenu;
require(["dojo/ready", "dijit/Menu", "dijit/MenuItem", "dijit/PopupMenuItem"], function (ready, Menu, MenuItem) {
ready(function () {
pMenu = new Menu({
targetNodeIds: ["progmenu"]
});
pMenu.addChild(new MenuItem({
label: "Simple menu item"
}));
pMenu.startup();
});
});
function runAfterDelay(fn, ms) {
setTimeout(function () {
console.log("Timeout " + new Date());
fn();
}, ms);
}
添加事件处理。
dijit/popup.close()
,将菜单作为单个参数传递。blur
事件以“欺骗”菜单关闭。...
require(["dojo/dom", "dojo/on", "dijit/popup"], function (dom, on, popup) {
var timeout = 3000;
on(dom.byId("btnPopupClose"), "click", function () {
runAfterDelay(function () {
if (pMenu) {
console.log("Calling .close()");
popup.close(pMenu);
}
}, timeout);
});
on(dom.byId("btnTriggerBlur"), "click", function () {
runAfterDelay(function () {
var opts = {
bubbles: true,
cancelable: true
};
console.log("emitting blur event");
on.emit(pMenu.domNode, "blur", opts);
}, timeout);
});
});