关闭图标不会出现

时间:2011-06-02 05:05:17

标签: javascript popup

我试图在我的页面上添加一个文本弹出框,这段代码帮助了我,但我需要添加一个关闭图标(这是我代码中的图像)..

但它不起作用:/

这是我的代码:

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;
}

可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

这应该意味着src的路径是错误的。即,../../Images/closebox.png

将此添加到您的代码中,看看是否有效

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

即使这不起作用,它也会向您显示代码中的其他错误。

alt属性始终添加到img标记是一种非常好的做法。

更新

我刚看到这个inClosebox.style.visibility = 'hidden'; 你正在追加img,那么当父母被隐藏时你怎么可能让它可见? 甘拜下风。或者你有额外的代码?如果不是,请删除该行并尝试。

相关问题