JS如何在 Body 区域插入内容

<html>
<head>
<script type="text/javascript">
function getElements()
{
var x=document.getElementById("body");
x.innerHTML="Hello World!";
alert('OK')
}
</script>
</head>
<body>
<input type="button" onclick="getElements()" value="How many input elements?" />
<span id="spa"></span>
</body>
</html>

为什么不行额

这是IE兼容性的问题,换一种写法:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>图片轮播</title>
</head>
<body>
<TABLE cellSpacing=0 cellPadding=0 width=750 align=center border=0>
<TBODY>
<TR>
<TD><style type="text/css">
<!--
#demo {
overflow:hidden;
border: 0px dashed #CCC;
width: 970px;
height: 155px;
}
#demo img {
border: 3px solid #F2F2F2;
}
#indemo {
float: left;
width: 800%;
}
#demo1 {
float: left;
}
#demo2 {
float: left;
}
-->
</style>

<div id="demo">
<div id="indemo">
<div id="demo1">
<table width="180" border="0" cellspacing="2" cellpadding="0">
<tr>
<td><table width=880 border="0" cellpadding="0" cellspacing="3">
<tr>
<td align="left"><table border="0" cellpadding="0" cellspacing="2" style="border:1px solid #B5B5B5" onMouseOver="this.style.border='1px solid #ebd1aa'" onMouseOut="this.style.border='1px solid #B5B5B5'">
<tr>
<td width="160" height="120" align="center" valign="middle">1</td>
</tr>
</table></td>
<td align="left"><table border="0" cellpadding="0" cellspacing="2" style="border:1px solid #B5B5B5" onMouseOver="this.style.border='1px solid #ebd1aa'" onMouseOut="this.style.border='1px solid #B5B5B5'">
<tr>
<td width="160" height="120" align="center" valign="middle">2</td>
</tr>
</table></td>
<td align="left"><table border="0" cellpadding="0" cellspacing="2" style="border:1px solid #B5B5B5" onMouseOver="this.style.border='1px solid #ebd1aa'" onMouseOut="this.style.border='1px solid #B5B5B5'">
<tr>
<td width="160" height="120" align="center" valign="middle">3</td>
</tr>
</table></td>
<td align="left"><table border="0" cellpadding="0" cellspacing="2" style="border:1px solid #B5B5B5" onMouseOver="this.style.border='1px solid #ebd1aa'" onMouseOut="this.style.border='1px solid #B5B5B5'">
<tr>
<td width="160" height="120" align="center" valign="middle">4</td>
</tr>
</table></td>
<td align="left"><table border="0" cellpadding="0" cellspacing="2" style="border:1px solid #B5B5B5" onMouseOver="this.style.border='1px solid #ebd1aa'" onMouseOut="this.style.border='1px solid #B5B5B5'">
<tr>
<td width="160" height="120" align="center" valign="middle">5</td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table>
</div>
<div id="demo2"></div>
</div>
</div>

<script>
<!--
var speed=10; //数字越大速度越慢
var tab=document.getElementById("demo");
var tab1=document.getElementById("demo1");
var tab2=document.getElementById("demo2");
tab2.innerHTML=tab1.innerHTML;
function Marquee(){
if(tab2.offsetWidth-tab.scrollLeft<=0)
tab.scrollLeft-=tab1.offsetWidth
else{
tab.scrollLeft++;
}
}
var MyMar=setInterval(Marquee,speed);
tab.onmouseover=function() ;
tab.onmouseout=function() ;
-->
</script>
</TD></TR></TBODY></TABLE></TD></TR></TABLE>
</BODY></HTML>
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-10-09
<html>
<head>
<script type="text/javascript">
function getElements(){
  var x=document.getElementById("span");//这里是向body的<span id="spa"></span>中插内容
  x.innerHTML="Hello World!";
  alert('OK')
  }
</script>
</head>
<body>
<span id="spa"></span>
<input type="button" onclick="getElements()" value="How many input elements?" />
</body>
</html>

相似回答