2009

Oct

17

IE中只读的innerHTML属性
文章分类:XHTML相关

       编写ajax程序时,经常需要动态生成页面元素,而element的属性innerHTML就是我们经常利用的途径。但今天在为一个table元素的innerHTML时赋值时,在firefox下是好的,但在ie下却没反应,代码如下:

document.getElementById("newline").innerHTML = document.getElementById("newline").innerHTML + tmpstr;

在网上找了一下资料,原因是在ie下,下列元素table,thead,tfoot,tbody,tr,col,colgroup,html,title,style,frameset的innerHTML属性是只读的(注意没有td),所以没法改变,如果想要在ie实现像Firefox下面的效果,需要用jQuery,写法如下:

var temp = $("#idname").html() + tmpstr;  
$("#idname").html(temp);  

下面来看一个实例:

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>IE中只读的innerHTML属性</title>  
<script language="javascript" type="text/javascript" src="jquery.js"></script>  
<script language="javascript" type="text/javascript">  
function newadd(){  
    var name = document.getElementById("name").value;  
    var email = document.getElementById("email").value;  
    var tmpstr = "<tr  width='100%'>";  
    tmpstrtmpstr = tmpstr + "<td  width='50%'>"+name+"</td>";  
    tmpstrtmpstr = tmpstr + "<td width='50%' >"+email+"</td>";  
    tmpstrtmpstr = tmpstr + "</tr>";  
    /*兼容ie与Firefox*/  
    var temp = $("#idname").html() + tmpstr;  
    $("#idname").html(temp);  
    /*只兼容Firefox*/  
    document.getElementById("newline").innerHTML = document.getElementById("newline").innerHTML + tmpstr;  
}  
</script>  
</head>  
<body>  
<form>  
  <input id="name" type="text" />  
  <input id="email" type="text" />  
  <input type="button" value="提交" onclick="newadd()" />  
</form>  
<table width="320" border="0" cellspacing="0" cellpadding="0">  
  <tbody id="idname">  
  </tbody>  
</table>  
</body>

点击此处查看演示效果。

本站原创,转载请标明:来自追梦博客(http://www.wuleilei.com/)

本文地址:http://www.wuleilei.com/Blog/240

相关文章

精通JavaScript jQuery (2009-06-19)

JavaScript实现自动刷新 (2009-10-06)

让window.event.keyCode兼容Firefox (2009-10-12)

JavaScript返回上一页并自动刷新的方法 (2009-11-12)

JavaScript去除字符串两端空格 (2009-12-22)

JavaScript全选与取消全选 (2009-12-24)

一款很不错的JS日历插件 (2010-01-04)

JavaScript获取FCKeditor编辑器内容的方法 (2010-05-27)

javaScript与jquery实现锚点平滑移动 (2010-12-30)

JavaScript获取网页中HTML元素的几种方法 (2011-06-14)

看看他们的脚印

闲言碎语

晚上和爸妈吃个饭。 (2月6日)

装修好累哟 (2月4日)

《斯巴达克斯:复仇》 (2月3日)

文章分类

最近文章

iOS开发之回调delegate的方法...

iOS开发之使用AES加密(兼容Obj...

事情好多哦

iOS开发之获取本机IP地址的方法

简单记录PC上安装苹果系统

最近评论

日志归档

友情链接

其它功能

随机标签