关于HTML中插入JavaScript的问题

来源:百度知道 编辑:UC知道 时间:2024/05/28 08:36:13
下面的代码错在哪里?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 1</title>
</head>
<body>
<script language="javascript">
a=3; var pp=Math.random();var p1=Math.round(pp*(a-1))+1;
function c1()
{
this.src='';
this.border='';
this.alt='';
}
b=new Array()
for(var i=1;i<=a;i++)
{b[i] = new c1() }
b[1].src="1.gif";b[1].border="0";b[1].alt="";
b[2].src="2.gif";b[2].border="0";b[2].alt="";
b[3].src="3.gif";b[3].border="0";b[3].alt="";
var pic="";
pic + ='<img src'=+b[p1].src+'border="0"'+'alt='+b[p1].alt+'>';
document.write(pic);
</script>
</body>
刷新显示不同图片

pic + ='<img src'=+b[p1].src+'border="0"'+'alt='+b[p1].alt+'>';
这一句错了.
1. += 你的加号给等号之间多了个空格.
2. '<img src'=+b[p1].src 这个修改成 '<img src='+b[p1].src

修改后的代码:
pic +='<img src='+b[p1].src+'border="0"'+'alt='+b[p1].alt+'>';