希望使用原型将选项动态添加到 Select 元素。
似乎有很多不同的方法可以做到这一点,都与 options.add 等有关......在跨浏览器的方式中看不到太多。
希望它尽可能轻。
这就是我到目前为止所得到的。这只是附加我坚持的选项:
var oNewOption = new Element('option').value=vItem;
oNewOption.text=vItem;
有什么想法吗?
提前致谢!
希望使用原型将选项动态添加到 Select 元素。
似乎有很多不同的方法可以做到这一点,都与 options.add 等有关......在跨浏览器的方式中看不到太多。
希望它尽可能轻。
这就是我到目前为止所得到的。这只是附加我坚持的选项:
var oNewOption = new Element('option').value=vItem;
oNewOption.text=vItem;
有什么想法吗?
提前致谢!
不需要 Prototype,使用自 1990 年代中期以来适用于所有主要桌面浏览器的以下历史悠久的方法同样容易:
// Assuming a select element stored in a variable called 'select'
select.options[select.options.length] = new Option("Option text", "optionValue");
select.insert(new Element('option', {value: myValue}).update(myLabel));
insert追加到select对象的内容,update更新新选项对象的内容。
不过,并不比经典方式更好。