2011-06-02 51 views
0

我試圖在我的頁面上添加一個文本彈出框,並且此代碼幫助了我,但我需要添加一個關閉圖標(這是我的代碼中的圖像)。但它不工作:/關閉圖標不出現

這裏是我的代碼:

function show_hide_box(an, width, height, borderStyle) { 

if (navigator.userAgent.indexOf("MSIE") != -1) { 
    var browserIsIE = true; 
} else { browserIsIE = false; } 


var href = an.href; 
var boxdiv = document.getElementById(href); 

if (boxdiv != null) { 
if (boxdiv.style.display=='none') { 
    move_box(an, boxdiv); 
    boxdiv.style.display='block'; 
} else 
    boxdiv.style.display='none'; 
return false; 
} 

boxdiv = document.createElement('div'); 
boxdiv.setAttribute('id', href); 
boxdiv.style.display = 'block'; 
boxdiv.style.position = 'absolute'; 
boxdiv.style.width = width + 'px'; 
boxdiv.style.height = height + 'px'; 
boxdiv.style.border = borderStyle; 
boxdiv.style.backgroundColor = '#FFF'; 

var inClosebox = document.createElement("div"); 
inClosebox.setAttribute('id', 'Close'); 
inClosebox.style.position = 'absolute'; 

if (browserIsIE) { 
    inClosebox.style.left = '-1px'; 
    inClosebox.style.top = '0px'; 
} else { 
    inClosebox.style.left = '-15px'; 
    inClosebox.style.top = '-15px'; 
} 

inClosebox.style.visibility = 'hidden'; 

var inImage2 = document.createElement("img"); 
inImage2.onclick = function() { this.document.close(); }; 
inImage2.setAttribute('src', '../../Images/closebox.png'); 
inImage2.setAttribute('width', '30'); 
inImage2.setAttribute('height', '30'); 
inImage2.setAttribute('border', '0'); 
inImage2.style.cursor = 'pointer'; 
inClosebox.appendChild(inImage2); 


var contents = document.createElement('iframe'); 
contents.scrolling = 'yes'; 
contents.frameBorder = '0'; 
contents.style.width = width + 'px'; 
contents.style.height = height + 'px'; 
contents.src = href; 

boxdiv.appendChild(contents); 
boxdiv.appendChild(inClosebox); 
document.body.appendChild(boxdiv); 
move_box(an, boxdiv); 

return false; 
} 

可以在任何幫助我嗎?

+0

如果您使用的是Firefox,請使用/ [下載Firebug](http://getfirebug.com/)並嘗試將接近盒定位到您想要的位置,並且您可以在您的css中使用這些值 – Ibu 2011-06-02 05:11:15

+0

嘗試設置樣式一個類,只是給你的元素classNames – Ibu 2011-06-02 05:12:46

+0

我做..並使用螢火蟲。但代碼closebox完全不顯示。如果在我的代碼中出現錯誤,我會懷疑嗎? – Evanescence 2011-06-02 05:14:55

回答

1

這應該意味着src的路徑是錯誤的。即,../../Images/closebox.png

添加到您的代碼,看看這是否工作

inImage2.setAttribute('alt', 'Close'); 

即使這行不通,那就說明你,別的東西是錯誤的代碼。

它是一個很好的做法,總是添加alt屬性到img標籤。

更新:

我剛看到這個inClosebox.style.visibility = 'hidden'; 要附加img到,因此如何你打算怎麼可能使其可見當父是隱藏?
打我。或者你有額外的代碼?如果不是,請刪除該行並嘗試。

+0

它在正確的道路上,是的,我忘了設置知名度! 感謝naveen和伊布..但不幸的是, inImage2.onclick = function(){this.document.close(); }; 無法正常工作,請點擊圖標後用什麼方法使箱子關閉? – Evanescence 2011-06-02 05:49:49

+0

它現在有效,我確實使用:document.body.removeChild(boxdiv); thnx大家好! – Evanescence 2011-06-02 06:12:06

+0

不在了。很高興知道現在一切正常。 – naveen 2011-06-02 08:19:50