2008-11-14 68 views
8

我需要用JavaScript等價物替換我們的Ajax Modal Popup控件。我們將其用作簡單的上下文相關幫助類型彈出窗口。我做了一個快速瀏覽,但沒有看到我正在尋找的東西。我只需要一些文本和一個簡單的關閉按鈕/鏈接,但是我希望頁面在彈出窗口下面變暗,就像使用Ajax模式控件一樣。如何編寫JavaScript模式彈出窗口(替換Ajax)?

任何人都可以提出一個很好的JavaScript彈出/幫助類型的解決方案,你已經使用?

回答

24

我可以爲您提供代碼。根據需要進行修改,好嗎?

頁面的JavaScript:

function myPop() { 
    this.square = null; 
    this.overdiv = null; 

    this.popOut = function(msgtxt) { 
     //filter:alpha(opacity=25);-moz-opacity:.25;opacity:.25; 
     this.overdiv = document.createElement("div"); 
     this.overdiv.className = "overdiv"; 

     this.square = document.createElement("div"); 
     this.square.className = "square"; 
     this.square.Code = this; 
     var msg = document.createElement("div"); 
     msg.className = "msg"; 
     msg.innerHTML = msgtxt; 
     this.square.appendChild(msg); 
     var closebtn = document.createElement("button"); 
     closebtn.onclick = function() { 
      this.parentNode.Code.popIn(); 
     } 
     closebtn.innerHTML = "Close"; 
     this.square.appendChild(closebtn); 

     document.body.appendChild(this.overdiv); 
     document.body.appendChild(this.square); 
    } 
    this.popIn = function() { 
     if (this.square != null) { 
      document.body.removeChild(this.square); 
      this.square = null; 
     } 
     if (this.overdiv != null) { 
     document.body.removeChild(this.overdiv); 
     this.overdiv = null; 
     } 

    } 
} 

現在的HTML頁面,使用JavaScript文件:

<html> 
    <head> 
    <script type="text/javascript" src="NAME OF THE PAGE!.js"></script> 
    <style> 
     div.overdiv { filter: alpha(opacity=75); 
         -moz-opacity: .75; 
         opacity: .75; 
         background-color: #c0c0c0; 
         position: absolute; 
         top: 0px; 
         left: 0px; 
         width: 100%; height: 100%; } 

     div.square { position: absolute; 
        top: 200px; 
        left: 200px; 
        background-color: Menu; 
        border: #f9f9f9; 
        height: 200px; 
        width: 300px; } 
     div.square div.msg { color: #3e6bc2; 
          font-size: 15px; 
          padding: 15px; } 
    </style> 
    </head> 
    <body> 
    <div style="background-color: red; width: 200px; height: 300px; 
       padding: 20px; margin: 20px;"></div> 

    <script type="text/javascript"> 
     var pop = new myPop(); 
     pop.popOut("Jose leal"); 
    </script> 
    </body> 
</html> 

希望這可以幫助。

7

我已經使用了simplemodal jQuery插件,我對它很滿意。你可以看看here

0

我開發了一個名爲Msg的JavaScript庫。它允許輕鬆創建一個模式窗口/彈出窗口。它會在背後創建一個疊加層,使背景變暗。它沒有關閉按鈕,但可以通過單擊背景疊加來關閉它。