以下这段代码里,因为在三个地方加了 alt="" 而出错了,图片无法显示
<div id="desc">
<script type="text/javascript">
window.onload = function(){
var ImgList = new Array();
ImgList[0]='http://jingyan.baidu.com/event/img/jingcai295ps.jpg';
ImgList[1]='http://jingyan.baidu.com/event/img/jingcai195ps.jpg';
ImgList[2]='http://jingyan.baidu.com/event/img/jingcai294ps.jpg';
ImgList[3]='';
ImgList[4]='';
ImgList[5]='';
ImgList[6]='';
ImgList[7]='';
document.getElementById('big_image').innerHTML = '<img src="'+ImgList[0]+'" alt="" />';
var html = '';
for (var i in ImgList){
if(ImgList[i] != '')
{
html += "<div style='Border-style:double; Border-color:#dddddd; Border-width:3px; margin:2px; width:60px; height:60px; float:left'><img src='"+ImgList[i]+"' width='60' height='60' border='0' alt="" /></div>";
}
}
document.getElementById("image_list").innerHTML = html;
var imgs = document.getElementById("image_list").getElementsByTagName("img");
for(var i=0;i <8; i++)
{
   if (ImgList[i] != '') {
imgs[i].onmouseover=function(){
var a = this.src;
var big_image = document.getElementById('big_image');
big_image.innerHTML = "<img src='"+a+"' width='300' height='300' border='0' alt="" />";
}
}
}
}
</script>
<div style="width: 608px">
<div id="big_image" style="width: 300px"></div>
</div>
<div id="image_list" style="width: 140px"></div>
</div>
而如果把三个 alt=""去掉,成为以下代码,就可以正常运行
<div id="desc">
<script type="text/javascript">
window.onload = function(){
var ImgList = new Array();
ImgList[0]='http://jingyan.baidu.com/event/img/jingcai295ps.jpg';
ImgList[1]='http://jingyan.baidu.com/event/img/jingcai195ps.jpg';
ImgList[2]='http://jingyan.baidu.com/event/img/jingcai294ps.jpg';
ImgList[3]='';
ImgList[4]='';
ImgList[5]='';
ImgList[6]='';
ImgList[7]='';
document.getElementById('big_image').innerHTML = '<img src="'+ImgList[0]+'" />';
var html = '';
for (var i in ImgList){
if(ImgList[i] != '')
{
html += "<div style='Border-style:double; Border-color:#dddddd; Border-width:3px; margin:2px; width:60px; height:60px; float:left'><img src='"+ImgList[i]+"' width='60' height='60' border='0' /></div>";
}
}
document.getElementById("image_list").innerHTML = html;
var imgs = document.getElementById("image_list").getElementsByTagName("img");
for(var i=0;i <8; i++)
{
   if (ImgList[i] != '') {
imgs[i].onmouseover=function(){
var a = this.src;
var big_image = document.getElementById('big_image');
big_image.innerHTML = "<img src='"+a+"' width='300' height='300' border='0' />";
}
}
}
}
</script>
<div style="width: 608px">
<div id="big_image" style="width: 300px"></div>
</div>
<div id="image_list" style="width: 140px"></div>
</div>
被强制添加alt=""的三段是:document.getElementById('big_image').innerHTML = '<img src="'+ImgList[0]+'" alt="" />';html += "<div style='Border-style:double; Border-color:#dddddd; Border-width:3px; margin:2px; width:60px; height:60px; float:left'><img src='"+ImgList[i]+"' width='60' height='60' border='0' alt="" /></div>";big_image.innerHTML = "<img src='"+a+"' width='600' height='600' border='0' alt="" />";因为这个alt=""是我在上传代码到一个工具的时候,工具的 description editor 自动添加的,肯定会被加上去,我希望能找到一个办法,能修改一下代码,使不会因为 alt=""的出现而出错。非常感谢大家,会帮我很大一个忙。javascript图片

解决方案 »

  1.   

    alt=""  改成    alt=''   <div id="desc">
    <script type="text/javascript">
    window.onload = function(){
    var ImgList = new Array();
    ImgList[0]='http://jingyan.baidu.com/event/img/jingcai295ps.jpg';
    ImgList[1]='http://jingyan.baidu.com/event/img/jingcai195ps.jpg';
    ImgList[2]='http://jingyan.baidu.com/event/img/jingcai294ps.jpg';
    ImgList[3]='';
    ImgList[4]='';
    ImgList[5]='';
    ImgList[6]='';
    ImgList[7]='';
    document.getElementById('big_image').innerHTML = '<img src="'+ImgList[0]+'" alt="" />';
    var html = '';
    for (var i in ImgList){
    if(ImgList[i] != '')
    {
    html += "<div style='Border-style:double; Border-color:#dddddd; Border-width:3px; margin:2px; width:60px; height:60px; float:left'><img src='"+ImgList[i]+"' width='60' height='60' border='0' alt='' /></div>";
    }
    }
    document.getElementById("image_list").innerHTML = html;
    var imgs = document.getElementById("image_list").getElementsByTagName("img");
    for(var i=0;i <8; i++)
    {
       if (ImgList[i] != '') {
    imgs[i].onmouseover=function(){
    var a = this.src;
    var big_image = document.getElementById('big_image');
    big_image.innerHTML = "<img src='"+a+"' width='300' height='300' border='0' alt='' />";
    }
    }
    }
    }
    </script>
    <div style="width: 608px">
    <div id="big_image" style="width: 300px"></div>
    </div>
    <div id="image_list" style="width: 140px"></div>
    </div>