在 XUL 中动态添加和选择菜单项
我正在使用包含电话代码列表的 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>
但是,当我在页面上运行它时,菜单项会正确创建,但菜单的第一个元素不会已选择。我尝试在其中一个字段上设置选定的属性,但结果是相同的。我在这里缺少什么?
谢谢! Luka
更新:事实证明,我错误地设置了 selectedIndex。应该这样做: $('countryCode').selectedIndex = 10;
I'm using a Javascript object containing a list of phone codes to generate a dropdown menu in XUL. My object has this form:
var CountryCodes = {
"Afghanistan":"+93",
"Albania":"+355",
"Algeria":"+213"
}
The code for populating the menupopup looks like this:
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 );
and my XUL looks like this:
<menulist id="countryCode">
<menupopup id="countryCodePopup"></menupopup>
</menulist>
However, when I run it on the page, the menuitem gets created properly, but the first element of the menu doesn't get selected. I tried to set a selected attribute on one of the fields, but the result is the same. What am I missing here?
Thanks!
Luka
Update: As it turns out, I was setting selectedIndex incorrectly. It should have been done like so:
$('countryCode').selectedIndex = 10;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这样:
作为参考,请检查:
https://developer.mozilla.org/en/XUL_Tutorial/Manipulated_Lists
Try like this:
For reference, please check this:
https://developer.mozilla.org/en/XUL_Tutorial/Manipulating_Lists