select设置innerHMTL
select控件在标准浏览器下可以直接innerHTML设置内容,IE则不行。
HTML结构:
<form name="form1">
<select name="select1"></select>
</form>
先看直接使用select.innerHTML
var form = document.forms["form1"];
var select = form["select1"];
select.innerHTML = '<option value="1">1</option>';
运行发现标准浏览器如chrome, firefox运行正常,DOM树为

IE(678)全家都呵呵了:

原因在于IE使用innerHTML给select赋值时会根据/^<\w\d['" ]>&/(尖括号中间的字母、数字,引号,空格)匹配的字符都干掉,无力吐槽。
2种解决思路
1、使用select的outerHTML或者父级的innerHMTL
var select = document.forms["form1"]["select1"];
select.outerHTML = '<select name="select1"><option value="1">1</option></select>';
outerHMTL IE系列和chrome是支持的,新版的FireFox也支持,国内Firefox浏览器份额低的可以忽略不计。如果嫌弃outerHTML,当然可以换innerHTML
document.forms["form1"].innerHTML = '<select name="select1"><option value="1">1</option></select>';
简单暴力
2、使用new Option创建select的options,这是比较推荐的方法。
var form = document.forms["form1"];
var select = form["select1"];
select.options.add(new Option("text", "value")); // 或 select.add(new Option("text", "value"))
new Option创建一个option对象实例,依次接受text,value两个参数,text为option的文本值,value即为option的value值。
发散思维,几种常见的option操作做个汇总:
var form = document.forms["form1"];
var select = form["select1"]; // 增加
select.options.add(new Option("text1", "value1")); // 或 select.add(new Option("text1", "value1"))
select.options.add(new Option("text2", "value2")); // 或 select.add(new Option("text2", "value2")) // 删除选中项,也可指定删除
var index = select.options.selectedIndex;
select.options.remove(select.index); // 或 select.remove(select.index) // 全删
select.options.length = 0; // 改text
select.options[select.selectedIndex].text = "text3";
// 改value
select.options[select.selectedIndex].value = "value3";
// 同时修改text | value
select.options[select.selectedIndex] = new Option("text3", "value3"); // 查
var text = select.options[select.selectedIndex].text;
var value = select.options[select.selectedIndex].value;
select设置innerHMTL的更多相关文章
- input与select 设置相同宽高,在浏览器上却显示不一致,不整齐
遇到 input与select 设置相同宽高,在浏览器上却显示不一致,遂实验了下(IE 10.013 ,Firefox 30.0),得出以下结论 input width,height 值里面, 不 ...
- Go基础系列:为select设置超时时间
Go channel系列: channel入门 为select设置超时时间 nil channel用法示例 双层channel用法示例 指定goroutine的执行顺序 After() 谁也无法保证某 ...
- AngularJS的select设置默认值
AngularJS的select设置默认值 在使用Angular时候使用select标签时会遇到绑定数据指定默认显示值可这样实现 <!DOCTYPE html> <html ng-a ...
- select设置高度的兼容问题
在IE678下,我们给select设置高度的话,里面的option无法居中,折中的兼容方式就是,我们给select的border:0:外面套一层div,这个div给他设置padding,让select ...
- vue中select设置默认选中
vue中select设置默认选中 一.总结 一句话总结: 通过v-model来:select上v-model的值为option默认选中的那项的值(value) 二.select设置默认选中实例 < ...
- 为select 设置样式
问题: 在为表单添加下拉菜单时,有时候对菜单的样式没有特别的要求,就是需要修改下select元素的宽度和高度,但众所周知select元素的样式很难修改: select在各个浏览器,字体大小为14px时 ...
- easyui-combobox select 设置不分行(只显示在一行)
使用easyui 1.4.4 <select id="hotalid" class="easyui-combobox" data-options=&quo ...
- select设置text的值选中(兼容ios和Android)基于jquery
前一段时间改了一个bug,是因为select引起的.当时我没有仔细看,只是把bug改完了就完事了,今天来总结一下. 首先说option中我们通常会设置value的属性的,还有就是text值的,请参见下 ...
- 使用ant design组件时,Select设置mode="multiple"或mode="tags"时遇到问题:Uncaught Error: must set key for <rc-animate> children
import {Select} from 'antd'; <Select className={styles.edit_area_dialog_table_select_input_layout ...
随机推荐
- 关于可图化序列的一点结论 NEU 1429
Graphic Sequence A graphic sequence is a sequence of numbers which can be the degree sequence of som ...
- 飘雪圣域(icekingdom)
飘雪圣域(icekingdom) 题目描述 IcePrincess_1968 和 IcePrince_1968 长大了,他们开始协助国王 IceKing_1968 管理国内事物. IcePrinces ...
- 短文对话的神经反应机 -- Neural Responding Machine for Short-Text Conversation学习笔记
最近学习了一篇ACL会议上的文章,讲的是做一个短文对话的神经反映机, 原文: 会议:ACL(2015) 文章条目: Lifeng Shang, Zhengdong Lu, Hang Li: Ne ...
- C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件
我们 开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib, 就可以很容易的实现压缩和解压缩功能. 压缩文件: /// <summary> ...
- Javascript&Html-弹出窗口的屏蔽程序
大多数的浏览器都内置了弹出窗口的屏蔽程序,即使没有内置此类屏蔽程序的浏览器,用户也可以安装Yahoo tool等带有内置屏蔽程序的应用工具. 结果就是用户可以将绝大多数弹出窗口屏蔽掉. 于是,再弹出窗 ...
- [LeetCode] Add Two Numbers 链表
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 设置jenkins的邮件通知功能
1.进入系统配置页面配置邮件发送的SMTP 2. 进入项目配置页面,配置邮件通知:(每次不稳定构建时会邮件通知)
- LeetCode OJ-- Length of Last Word
https://oj.leetcode.com/problems/length-of-last-word/ 对一个字符串遍历,求最后一个单词的长度,如果有 ‘ ’,则切开了. 字符串的最后一个字符为 ...
- 四、Ubuntu 一些常用命令
1.锁定root用户 :sudo passwd -l root 2.解锁root用户 :sudo passwd -u root 3.切换身份:su root 或者 su 其他用户名,然后输入密码, ...
- C# 键值对的类型
一 C# 键值对类有以下类: ① IDictionary<string, Object> idc = new Dictionary<string, object>(); ...