5

我正在开发一个 Firefox 插件,我目前需要将菜单项动态添加到 menupopup 元素。我基本上尝试了 Mozilla 开发者中心的所有方法,但都没有奏效。

    function populateDropdown() {
    var counter = 0;
    for (var key in services) {
        var newMenuItem = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "menuitem");
        newMenuItem.setAttribute("label", services[key]['title'])

        document.getElementById("mainDropdown").appendChild(newMenuItem);
    }
}

此代码在 appendChild 命令处中断。任何想法为什么?

4

1 回答 1

5

您是否 100% 肯定 document.getElementById("mainDropdown") 返回非空结果?

试着把它分解成小块,然后添加一些调试代码:

var dropDown = document.getElementById("mainDropdown");
if(dropDown) {
  alert("dropDown found!");
  dropDown.appendChild(newMenuItem);
}
于 2009-06-19T14:57:59.490 回答