<html>
<head>
<script type="text/javascript">
function borda()
{
var h=document.createElement("h2");
var text=document.createTextNode("woshizhongguoren");
//alert(text.nodeValue);
h.appendChild(text);
alert(h.innerHTML);
document.firstChild.lastChild.lastChild.appendChild(h);
}
window.onload=borda;
</script>
</head>
<body><div><h1>我是中国人</h1></div><div></div></body></html>
帮忙看看原因。
<head>
<script type="text/javascript">
function borda()
{
var h=document.createElement("h2");
var text=document.createTextNode("woshizhongguoren");
//alert(text.nodeValue);
h.appendChild(text);
alert(h.innerHTML);
document.firstChild.lastChild.lastChild.appendChild(h);
}
window.onload=borda;
</script>
</head>
<body><div><h1>我是中国人</h1></div><div></div></body></html>
帮忙看看原因。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function borda(){
var h = document.createElement("h2");
var text = document.createTextNode("woshizhongguoren");
//alert(text.nodeValue);
h.appendChild(text);
alert(h.innerHTML);
if(navigator.userAgent.indexOf("MSIE")>0)
document.firstChild.lastChild.lastChild.appendChild(h);
else if(navigator.userAgent.indexOf("Firefox")>0)
document.firstChild.firstChild.nextSibling.firstChild.nextSibling.nextSibling.nextSibling.appendChild(h);
}
window.onload = borda;
</script>
</head>
<body>
<div>
<h1>我是中国人</h1>
</div>
<div></div>
</body>
</html>
在FF下,<body><div>之间的空白也是一个节点
function borda()
{var h=document.createElement("h2");
var text=document.createTextNode("woshizhongguoren");
//alert(text.nodeValue);
h.appendChild(text);
alert(h.innerHTML);
//document.firstChild.lastChild.lastChild.appendChild(h);//就是这里不兼容ff的
document.getElementsByTagName("div")[1].appendChild(h);
}
window.onload=borda;
</script>