gpt4 book ai didi

javascript - 在 XUL 中动态添加和选择菜单项

转载 作者:行者123 更新时间:2023-11-29 22:29:49 25 4
gpt4 key购买 nike

我正在使用包含电话代码列表的 Javascript 对象在 XUL 中生成下拉菜单。我的对象有这种形式:

var CountryCodes = {
"Afghanistan":"+93",
"Albania":"+355",
"Algeria":"+213"
}

填充 menupopup 的代码如下所示:

var docfrag = document.createDocumentFragment();

for( var country in CountryCodes ) {
var this_country = document.createElementNS(XUL_NS,'menuitem');
this_country.setAttribute( 'label', country );
this_country.setAttribute( 'value', CountryCodes[ country ] );
docfrag.appendChild( this_country );
}
$('countryCodePopup').appendChild( docfrag );
$('countryCode').setAttribute( 'selectedIndex', 0 );

我的 XUL 看起来像这样:

 <menulist id="countryCode">
<menupopup id="countryCodePopup"></menupopup>
</menulist>

但是,当我在页面上运行它时,菜单项已正确创建,但菜单的第一个元素未被选中。我试图在其中一个字段上设置一个 selected 属性,但结果是一样的。我在这里缺少什么?

谢谢!卢卡

更新:事实证明,我错误地设置了 selectedIndex。应该这样做: $('国家代码').selectedIndex = 10;

最佳答案

像这样尝试:

 document.getElementById("countryCode").selectedIndex = 10;

作为引用,请检查: https://developer.mozilla.org/en/XUL_Tutorial/Manipulating_Lists

关于javascript - 在 XUL 中动态添加和选择菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7588543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com