浏览器中立方式在javascript中为选择元素添加选项 [英] Browser Neutral Way to add options to a select element in javascript

查看:108
本文介绍了浏览器中立方式在javascript中为选择元素添加选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚学到了一些有趣的东西。 IE 6中javascript选择对象的add方法只接受一个参数。当你传递两个参数时,它会抛出一个错误,我认为这是标准,即使记录

I just learned something interesting. The add method for the javascript select object in IE 6 takes only one parameter. It throws an error when you pass it two parameters which I believe is the standard, even as documented on the MSDN site.

我的问题是这个。以编程方式将选项元素添加到javascript中的select元素的最佳实践建议是什么?

My question is this. What is your best-practice recommendation for programmatically adding option elements to a select element in javascript?


  • 根据值调用add方法用户代理?

  • 将select对象包装在提供正确方法签名的类中?

  • 使用javascript库为您执行此操作(指定哪个库)?

推荐答案

您可以使用try-catch块来执行此操作:

You can use a try-catch block for doing this :

              try
    {
        //Standards compliant
        list.add(optionTag, null);
    }
    catch (err)
    {
        //IE
        list.add(optionTag);
    }

这篇关于浏览器中立方式在javascript中为选择元素添加选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆