我无法弄清楚如何关闭一个简单的弹出窗口

时间:2011-12-19 18:17:45

标签: javascript popup

抱歉,这个简单的问题。 我不确定这一切是否重要,但我会提及以防万一。 :当用户点击链接时,会发生以下情况:

function begin(some info) is called->Makes an Ajax request to backend PHP->
PHP calls the callback function->Callback function calls another JS function that creates a popup->
In the popup creating function, The Save button is assigned a function that makes an ajax request and sends PHP some user input.->
PHP makes a callback to another function that does nothing.

这里,这是将弹出框组合在一起的功能。

function collection(name, description, data){
    var json = data;
    var cont = openSitePopup("400","300");
    //We create div objects.
    var namecell = ce("div","sitePopupCell");   
    //Stuff
    // We create a button and assign the createProducts Method to it.
    var sbtbutton = createBtn("submitObject","SAVE","createProducts()");
    //We append all the div objects to the popup.   
    cont.appendChild(namecell);
    //..
}

function openSitePopup(width,height) {

//try to center the popup if values are not passed
var xPos = (getWinWidth()/2) - (width/2) + sl;
var yPos = (getWinHeight()/2) - (height/2) + st;

sitepopupwin = ge("sitePopupWin");
clearElement(sitepopupwin);

var winhandle = ce("div","","sitePopupHandle");

sitepopupwin.style.display = "block";
//Other style assignments.  

//closebutton
var close = ce("img","sitePopupCloseBtn");
close.setAttribute("src",theme_path + "/images/icons/close.png");

//start adding goodies
var mydiv = ce("div","sitePopupContainer");

winhandle.appendChild(close);
winhandle.appendChild(createCleaner());
sitepopupwin.appendChild(winhandle);
sitepopupwin.appendChild(mydiv);

//return reference to the container for adding stuff
return mydiv;
}

function createProducts()
{
    //Some exciting Ajax stuff with a callback to writeNewFolder()
}

function writeNewFolder(data){
    //Nothing of interest here...yet.
}

现在,当我按下弹出窗口上的Save按钮时,请求成功,但弹出窗口没有关闭。我知道Window.Close()函数,但它不起作用。 cont.Close()似乎是一个明显的选择,但在这种情况下,我收到此错误:Uncaught TypeError: Object #<HTMLDivElement> has no method 'Close'因此,Cont只是一个div元素。 openSitePopup()方法仅返回div。

感谢您的帮助。

编辑:

function ce(elementType,elementClass,elementId,txt) {

var e = document.createElement(elementType);

//add optional parameters
if (elementId) e.setAttribute("id",elementId);
if (elementClass) setClass(e,elementClass);

//append extra text.  If passed an object, append with without the textnode wrapper
if (isData(txt)) {
    if (typeof(txt)=="object") e.appendChild(txt);
    else e.appendChild(ctnode(txt));
}

return e;

}

希望原始创作者不介意我在这里发布他的一些代码。嗯,它是免费和开源的,所以它应该没问题......我想。

编辑2:ge功能。这个很简单。

function ge(element) {
return document.getElementById(element);
}

1 个答案:

答案 0 :(得分:1)

您没有发布创建不是window弹出窗口的函数ge和ce,因此您无法.close()

ge('sitePopupWin').style.display='none';

var popup = ge('sitePopupWin');
popup.parentNode.removeChild(popup);