2016-03-07 146 views
0

我有一個簡單的彈出窗口(沒有任何內容),它將加載'onclick'事件。如何修改以下代碼以使頁面加載時彈出窗口的顯示方式相同?在頁面加載時顯示JavaScript PopUp

<head> 
    <title>Popup box</title> 
    <script type="text/javascript"> 
     <!-- 
      function showPopUpBox(id) { 
       var e = document.getElementById(id); 
       if(e.style.display == 'block') 
        e.style.display = 'none'; 
       else 
        e.style.display = 'block'; 
      } 
     //--> 
    </script> 
</head> 

<body> 

    <div id="popupBoxPosition"> 
     <div class="popupBoxWrapper"> 
      <div class="popupBoxContent"> 
       <h3>Popup Box</h3> 
       <p>Form will be held here</p> 
       <p>Click <a href="javascript:void(0)" onclick="showPopUpBox('popupBoxPosition');">here</a> to close popup box</p> 
      </div> 
     </div> 
    </div> 
    <div id="wrapper"> 
     <p>Click <a href="javascript:void(0)" onclick="showPopUpBox('popupBoxPosition');">here</a> to see popup box</p> 
    </div><!-- wrapper end --> 
</body> 

+0

呼叫在'$函數(的document.ready(函數(){...}))' – Teemu

回答

0
<script type="text/javascript"> 
window.onload = function(){ 



      function showPopUpBox(id) { 
       var e = document.getElementById(id); 
       if(e.style.display == 'block') 
        e.style.display = 'none'; 
       else 
        e.style.display = 'block'; 
      } 

      showPopUpBox(); 

} 

</script> 
+0

這將防止在線處理程序來調用'showPopUpBox'。 – Teemu

+0

你如何打電話? –

+0

你可以看到在HTML中有一些與'onclick =「showPopUpBox(...)'」屬性的鏈接。您已將'showPopUpBox'的作用域限制爲負載處理函數,現在單擊這些鏈接會導致錯誤。 – Teemu