2010-07-29 118 views
1

我跟着Jeremy Keith的书“DOM Scripting”,我试图让一个小图片库在Internet Explorer 6/7中正确呈现,我碰到了一堵墙。Internet Explorer Javascript Image问题

我有4个缩略图使用下面的代码点击时负载的占位符:

function showPic(whichpic) { 
    if (!document.getElementById("placeholder")) return true; 
    var source = whichpic.getAttribute("href"); 
    var placeholder = document.getElementById("placeholder"); 
    placeholder.setAttribute("src",source); 
    if (!document.getElementById("description")) return false; 
    var text = whichpic.getAttribute("title") ? whichpic.getAttribute("title") : ""; 
    var description = document.getElementById("description"); 
    if (description.firstChild.nodeType == 3) { 
     description.firstChild.nodeValue = text; 
    } 
    return false; 
} 

我的占位符插入使用createElement("img")的页面,但是当我点击缩略图图像压缩规模它替换的占位符。

function preparePlaceholder() { 
    if(!document.createElement) return false; 
    if(!document.createTextNode) return false; 
    if(!document.getElementById) return false; 
    if(!document.getElementById("imagegallery")) return false; 
    var placeholder = document.createElement("img"); 
    placeholder.setAttribute("id","placeholder"); 
    placeholder.setAttribute("src","images/placeholder.gif"); 
    placeholder.setAttribute("alt","Gallery Placeholder"); 
    var description = document.createElement("p"); 
    description.setAttribute("id","description"); 
    var desctext = document.createTextNode("Choose an image"); 
    description.appendChild(desctext); 
    var gallery = document.getElementById("imagegallery"); 
    insertAfter(placeholder,gallery); 
    insertAfter(description,placeholder); 
} 

因此,如下图所示的结果是扭曲的图像:

In Chrome In Explorer

直播网站在这里:http://anarchist.webuda.com/

的Javascript这里:http://pastebin.com/kaAhjdqk HTML浏览:这里http://pastebin.com/Dm5p2Dj6 CSS :http://pastebin.com/yiVPiQZe

+0

怎么样的CSS? – 2010-07-29 06:25:00

+0

加入:http://pastebin.com/yiVPiQZe – Ash 2010-07-29 06:27:59

回答

1

尝试增加了​​功能在接下来的2行之后placeholder.setAttribute("src",source);

placeholder.removeAttribute('width'); 
placeholder.removeAttribute('height'); 

使用IE8开发工具(兼容模式),我可以重现该问题,并发现widthheight被自动分配。所以我用调试器使用removeAttribute进行了测试,并且图像增长到了适当的大小。但我真的不知道这些线路的位置是否正确,请告诉我。

+0

修正了它。 谢谢! – Ash 2010-07-30 00:48:23