JavaScript函数调用给出意想不到的奇怪结果。

我正在开发一个使用nodejssocket.io的棋盘游戏

我有一个board.html文件,其中有一个板,聊天框和游戏统计。 该板有三层首先是板canvas( z-index:0 ),典当canvas( z-index:1 )和(最初隐藏, z-index:2iframepopup一个模式,当有东西要显示用户。

我在board.html文件中添加了一个button[show card],并创build了一个只包含模式的modal.html文件。 每当用户点击button, showCard()函数被调用, modal.html文件被设置为popup模式的iframe。 在我的modal.html文件中,我有一个代码,如果用户在模态外单击,则会删除模态。 现在一切正常,现在我再次调用showCard()函数后,当用户在模态外单击时删除模式。

用户在模式外单击的代码

 window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; //Removes the modal, works! showCard(); //After this call the function is being called but somehow the modal is not showing up again. } }; 

问题:我预计模态将被删除,并会再次显示。 但不知何故,它只是被删除。 showCard()函数正在调用,但模式不再显示。

showCard()函数:

 function showCard(){ document.getElementById("cardframe").style.display = ""; document.getElementById("cardframe").src = "modal.html"; } 

为了debugging目的,我在showCard()函数的第一行添加了一条警告消息。 现在,当我点击button时,先显示警报,然后显示模态。 然后,当我通过点击模式外的方式取消模态时,再次显示警告(确认showCard()函数被调用),但模态不显示。

我真的不明白为什么相同的一段代码是一次显示模式,而不是再次。

我在github上有这个代码(现在让我来执行),如果有人想看看它的话。

随意build议编辑。 如果有什么不清楚,请留言。

当你第二次调用showCard()时,它会执行得非常好: iframe显示出来, modal.html被加载,但是你的模式的divid="myModal" (从你的GitHub代码在modal2.html)仍然隐藏所以没有显示:

 modal.style.display = "none"; //Removes the modal, works! 

最好的解决scheme(我认为)将隐藏和显示只有id="cardframe"iframe