Javascript 关闭图标不';不出现

Javascript 关闭图标不';不出现,javascript,popup,Javascript,Popup,我试图在我的页面上添加一个文本弹出框,这段代码帮助了我,但我需要添加一个关闭图标(这是我代码中的一个图像) 但它不起作用:/ 这是我的密码: function show_hide_box(an, width, height, borderStyle) { if (navigator.userAgent.indexOf("MSIE") != -1) { var browserIsIE = true; } else { browserIsIE = false; } var href =

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

但它不起作用:/

这是我的密码:

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

有什么可以帮我的吗?

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

将其添加到您的代码中,然后查看是否有效

inImage2.setAttribute('alt', 'Close');
即使这不起作用,它也会向您显示代码中存在其他问题

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

更新:

我刚刚看到这个
inClosebox.style.visibility='hidden'
您正在添加
img
,那么当父对象被隐藏时,您将如何使其可见?

我不知道。或者你有额外的代码吗?如果没有,请删除该行并重试。

如果您使用的是firefox,请使用/并尝试将closebox定位到您想要的位置,您可以在csstry中使用这些值来设置类中的样式,只需为元素指定ClassName即可。。一定要用萤火虫。但是代码关闭框根本没有出现。我不知道我的代码中是否有错误?就像naveen建议的那样,检查图像是否在正确的路径中如果我想从页面中检索特定部分,我可以这样做吗?我的意思是:var href=an.href;var-boxdiv=document.getElementById(href);我可以指定标记的ID来检索它吗?比如:var href=an.href;var-boxdiv=document.getElementById(href.getElementById(“显示此部分!”)??它在正确的路径上,是的,我忘了设置可见性!谢谢纳文和伊布。。但不幸的是,inImage2.onclick=function(){this.document.close();};无法正常工作,单击图标后,我应该使用什么来关闭该框?现在可以工作了,我使用了:document.body.removeChild(boxdiv);谢谢大家!我还有一个问题:$我已经在上面的评论部分写了它。你可以把它作为一个单独的问题发布吗。我想再详细一点。当然,我: