[英]Internet Explorer Javascript Image Problem
我遵循Jeremy Keith的《 DOM腳本》一書,在嘗試獲得一個小的圖像庫以在Internet Explorer 6/7中正確呈現時遇到了麻煩。
我使用以下代碼在單擊時會在占位符中加載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);
}
因此,結果如下圖所示:
此處的實時網站: http : //anarchist.webuda.com/
此處的Javascript: http : //pastebin.com/kaAhjdqk HTML此處: http : //pastebin.com/Dm5p2Dj6 CSS此處: http : //pastebin.com/yiVPiQZe
嘗試在placeholder.setAttribute("src",source);
之后的兩行中添加到showPic
函數placeholder.setAttribute("src",source);
placeholder.removeAttribute('width');
placeholder.removeAttribute('height');
使用IE8開發人員工具 (在兼容模式下),我可以重現該問題,並發現width
和height
是自動分配的。 因此,我使用removeAttribute
與調試器進行了測試,並且圖像增長到了適當的大小。 但是我真的不知道這些行的位置是否正確,所以讓我知道。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.