js中createElement方法的兼容性
来源:互联网 发布:在这网络里认识你歌词 编辑:程序博客网 时间:2024/06/06 01:34
例如:document.createElement("td");//创建一个td
若 tagName 中出现不合法的字符,则应抛出 INVALID_CHARACTER_ERR 异常。
由于微软的强大,其产品IE使用了一些规范以外的规则,其中之一就与createElement方法有关。即:在 IE 中,createElement 方法不仅可以通过合法的标签名创建节点对象,还可以通过传入一段合法的 HTML 代码字符串作为参数创建节点对象。
例如:document.createElement("<input type='text' name='txtName'>");//创建一个name属性为"txtName"的input
若使用了 IE 特有的通过为 createElement 传入一段合法的 HTML 代码字符串作为参数创建节点对象的方法,则在其他浏览器中将会抛出异常,并导致后续代码无法执行。这就造成了浏览器的兼容性问题。
也许有的读者会说,用 createElement 方法时,使用传入标签名的做法不就解决了兼容性问题吗?
例如:
var txt = document.createElement("input");
txt.type = "text";
txt.name = "txtName";
......
但在 IE6 , IE7 中,如果动态生成 input 元素,是无法为其设置 name 属性的,这是IE自身的问题。这个时候,IE 特有的通过为 createElement 传入一段合法的 HTML 代码字符串作为参数创建节点对象的方法,就有了大显身手的机会。不过,IE 中无法为 input 设置 name 属性的 bug 已经在 IE8 中被修复。在其他浏览器中,不存在上述问题。
所以,要最终解决 createElement 方法的兼容性问题,还是要注意判断浏览器,针对 IE 可以使用其特有的通过为createElement 传入一段合法的 HTML 代码字符串作为参数的方法,非 IE 浏览器仍然使用 W3C 规范的标准方法。
例如:
if($.browser.msie) {
var txt = document.createElement("<input type='text' name='txtName'>")
} else {
var txt = document.createElement("input");
txt.type = "text";
txt.name = "txtName";
}
如果radio类型的的input,当要自动生成多个radio时,每个radio的name必须是不同的,这时需要是name的变化:
//判断浏览器,选择使用两种DOM生成方式:
if(navigator.userAgent.indexOf("MSIE")>0){
num++;
var radioname="command"+num;
radio_i = document.createElement('<input name='+radioname+'>');
//此处的radioname是随num的增长而增长的, 如 name=command1,command2,command3 ........一定要注意参数出入的格式'<input name='+radioname+'>';
radio_i.type="radio";
radio_i.value=arr_value[i];
} else {
//Firefox的方式:
radio_i = document.createElement("input");
radio_i.type="radio";
radio_i.value=arr_value[i];
radio_i.name="command"+num;
}
- js中createElement方法的兼容性
- js中createElement的使用
- js中createElement的使用
- JS中document.createElement()的用法
- JS中document.createElement()的用法
- JS中document.createElement()的用法
- 不为人知的JS调用样式的方法---document.createElement().addRule(..)
- js中setAttribute 的兼容性
- js中setAttribute 的兼容性
- js中setAttribute 的兼容性
- js document.createElement()的用法
- js、document.createElement()的用法
- 【转】JS写图层方法.document.createElement("div")
- JS中document.createElement()用法及注意事项
- js中创建元素createElement\createTextNode\appendChild
- js 使用js的CreateElement方法创建元素、样式表动态引入
- js 中getElementsByClassName的兼容性写法
- JS 中的appendChild和createElement的用法
- java 调用其它程序
- linux/unix下setuid/seteuid/setreuid/setresuid
- package命名空间详解及Annotation配置struts
- Linux系统Vim基本命令
- hdu3635
- js中createElement方法的兼容性
- 转载_linux kill信号列表
- 记录下 linux安装织梦cms遇到的问题和解决方法
- CreateEvent 和OpenEvent时事件全局名称问题 Global
- IN和EXISTS的详解(精品)
- Error parsing XML: junk after document element
- 状态栏的一些操作
- 优化思路整理-2-锁
- AspNetPager属性