admin管理员组文章数量:1196574
What is the preferred way in Javascript to dynamically create DOM option elements? I've found both the Option constructor and the createElement variant used in actual code like this:
var option = new Option(text, value);
and this:
var option = document.createElement('option');
option.text = text;
option.value = value;
Are there any drawbacks/compatibility issues with any of those methods? Also, are there any other methods to create options dynamically that should be preferred to the above for some reasons?
What is the preferred way in Javascript to dynamically create DOM option elements? I've found both the Option constructor and the createElement variant used in actual code like this:
var option = new Option(text, value);
and this:
var option = document.createElement('option');
option.text = text;
option.value = value;
Are there any drawbacks/compatibility issues with any of those methods? Also, are there any other methods to create options dynamically that should be preferred to the above for some reasons?
Share Improve this question asked Apr 22, 2013 at 10:06 GOTO 0GOTO 0 47.6k25 gold badges138 silver badges164 bronze badges2 Answers
Reset to default 13There are no differences between the two methods that I know of. Using the Option
constructor allows you to conveniently set the value and the text of the option, but you can do the same using the value
and text
properties.
There could have been the innerHTML
way, but IE8 and older fail hard on this...
I noticed for example that using new Option() doesn't work well under IE9 where it works in IE10 and IE11. I recently had go back to the original code and revert the change somebody did to go back using document.createElement('option') in order for IE9 to work.
本文标签: createelementDynamically create option elements in JavascriptStack Overflow
版权声明:本文标题:createelement - Dynamically create option elements in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738540510a2095392.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论