0

我无法找到关闭或重新加载由以下代码生成的弹出框的方法。我想避免发生的事情是,如果用户没有手动关闭窗口然后选择另一个“提示”,则新提示只是附加到现有文本中。期望的结果是;

  1. 当窗口失去焦点时窗口关闭,从而重置它。
  2. 粘贴新提示时,我可以“刷新”窗口。

我做了广泛的研究,尝试了很多不同的方法,但都无济于事。

请记住,我在一个严格的政府环境中工作,他们对安装第三方工具(如 jQuery 或类似工具)非常暴躁,所以我真的坚持使用这里的基础知识,直到我能说服他们不这样做。此外,对于 MOST 部分,部门中的大多数用户将使用 IE8 或更高版本,但(作为较低优先级)我宁愿开发一种不限制特定浏览器的解决方案。

也许有人可以建议一种更好的方法来做我正在做的事情,还是要求太多?;-) 提前致谢。

function PromptTip(title, message)
{
var HTMLTitle = "<p style='text-align:center'><b>" + title + "</b></p>"
var HTMLMsg = "<p style='text-align:center'>" + message + "</p>"
win = window.open("","Tip","width=320,height=210,scrollbars=no,resizable=no,titlebar=no");
win.moveTo(screen.width/2-160,screen.height/2-105)
win.document.writeln("<html><head><title>Prompt Tip</title></head><body onfocusout='javascript:close()'>");
win.document.writeln(HTMLTitle);
win.document.writeln(HTMLMsg);
win.document.writeln("<p style='text-align:center'><a href='javascript:close()'>close</a></p></body></html>");
}
4

2 回答 2

1

我强烈建议不要使用 window.open 和 window.document.writeln。代替 window.open,我建议动态创建标记(或显示隐藏的标记片段)并像所有现代对话框/模态 JS 库一样使用绝对定位。代替 win.document.writeln,我建议使用document.createElement创建 DOM并使用Node.appendChild(或任意数量的 DOM 插入方法)或使用标记模板方法进行插入。

也就是说,每次单击工具提示时,您都可以简单地修改代码以替换 iframe 主体的整个 innerHTML。

示例:http: //jsfiddle.net/ruJqj/

function PromptTip(title, message) {
    var HTMLTitle = "<p style='text-align:center'><b>" + title + "</b></p>"
    var HTMLMsg = "<p style='text-align:center'>" + message + "</p>"
    win = window.open("", "Tip", "width=320,height=210,scrollbars=no,resizable=no,titlebar=no");
    win.moveTo(screen.width / 2 - 160, screen.height / 2 - 105)

    win.document.body.innerHTML = HTMLTitle + HTMLMsg + "<p style='text-align:center'><a href='javascript:close()'>close</a></p>";
}​
于 2012-08-24T04:13:16.480 回答
0

如果我了解您的问题可能会有所帮助,请更改此行:

    win.document.writeln("<html><head><title>Prompt Tip</title></head><body onfocusout='javascript:close()'>");

对于这个:

    win.document.writeln("<html><head><title>Prompt Tip</title></head><body onBlur='javascript:close()'>");

你需要使用这个事件:onBlur='javascript:close()'

*我只是在 IE 9 中测试它,它工作正常

于 2012-08-24T04:06:37.700 回答