动态删除 XUL 菜单
我遇到一个问题,无法删除在扩展程序中动态创建的菜单。我可以使用 document.getElementById
或使用下面的 jquery 内容来获取菜单对象。基本上,用户单击“删除”菜单项,并且应该删除父菜单(有 2 个父菜单,因为它进入 menu > popup > menuitem (clicked) )。
警报会针对我想要删除的内容输出正确的id
。 removeObj.remove();
使扩展程序崩溃。最后一行似乎没有执行任何操作(除非第二次单击“删除”,它不再能找到 id
),但它在 UI 中仍然可见。
代码:
var jObj = $(menuObject);
var removeObj = $(jObj).parent().parent();
var id = removeObj.attr('id');
alert(id);
//removeObj.remove();
$(removeObj).parent().removeChild(document.getElementById(id));
这不是由于jquery。我已经写了这个,它在执行后完全禁用右键菜单(但在 if-case 中返回正确的 id 位置)
var mainMenu = document.getElementById('rclickMenu_MenuPopupContainer');
var toRemove = document.getElementById("rclickMenu_"+main);
for(var i=0; i < mainMenu.children.length; i++){
if(mainMenu.children[i].getAttribute('id') == toRemove.getAttribute('id')){
alert(toRemove.getAttribute('id'));
//mainMenu.removeItemAt(i);
mainMenu.removeChild(toRemove);
}
}
I'm having an issue where I can't remove a menu that was created dynamically in my extension. I can get the menu object with document.getElementById
or by using the jquery stuff like I have below. Basically, the user clicks the 'remove' menu item, and it's supposed to remove the parent menu (there are 2 parents because it goes menu > popup > menuitem (clicked) ).
Alert spits out the proper id
for what I want to remove. removeObj.remove();
crashes the extension. The last line doesn't seem to do anything (except if click remove the second time it no longer can find the id
), however it's still visible in the UI.
Code:
var jObj = $(menuObject);
var removeObj = $(jObj).parent().parent();
var id = removeObj.attr('id');
alert(id);
//removeObj.remove();
$(removeObj).parent().removeChild(document.getElementById(id));
it's not due to jquery. i have written this and it completely disables the right click menu after execution (but returns the correct id location in the if-case)
var mainMenu = document.getElementById('rclickMenu_MenuPopupContainer');
var toRemove = document.getElementById("rclickMenu_"+main);
for(var i=0; i < mainMenu.children.length; i++){
if(mainMenu.children[i].getAttribute('id') == toRemove.getAttribute('id')){
alert(toRemove.getAttribute('id'));
//mainMenu.removeItemAt(i);
mainMenu.removeChild(toRemove);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
做:
Do: