2013-03-26 38 views
1

我一直在使用blockUI彈出一個非常簡單的模式,並詢問用戶是否具有特定年齡。我總是試着在自己的頁面中開發一段代碼以避免衝突,這就是我在這裏所做的。我有一個簡單的html/javascript頁面,它不能正常運行。

每當頁面加載時,它就會出現。但是,試圖解鎖時(即使不使用按鈕),它什麼也不做。它只是與加載圖標坐在那裏。

<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
    <script type="text/javascript" src="jquery.blockUI.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      // A bit of a work-around. BlockUI tries to center based on location of 
      // the div. By attaching block to the html tag immediately, we avoid 
      // this issue completely. 
      $('html').block({ 
       message: $('#message'), 


       centerX: true, 
       centerY: true, 

       css: { 
        width: '600px', 
        height: '300px', 
        border: '3px solid #FF9900', 
        backgroundColor: '#000', 
        color: '#fff', 
        padding: '25px' 
       } 
      }); 

      $('#over').click(function() { 
       alert('clicked!'); 
       $.unblockUI(); 
       return false; 
      }); 

      $('#under').click(function() { 
       $.unblockUI(); 
       return false; 
      }); 

     }); 
    </script> 
</head> 

<body> 
<div id="message" style="display:none;"> 
    <img src="logo.png" border="0" /> 
    <br /> 
    <h1>Welcome!</h1> 

    You may not view this page unless you are 21 or over.<br /> 

    <button id="over">I am 21 or over</button>&nbsp;&nbsp;&nbsp;<button id="under">Under 21</button> 


</div> 
It's dusty under here! Let me be seen! 
</body> 
</html> 

沒有錯誤顯示在Chrome的控制檯,我真的找不到一個原因,爲什麼這不應該關閉。

回答

2

當你打電話$.unblockUI()嘗試調用它的元素,而不是,例如。 $('html').unblock();

<div class="body"> 
    <div id="message" style="display:none;"> 
     <img src="logo.png" border="0" /> 
     <br /> 
     <h1>Welcome!</h1> 
     You may not view this page unless you are 21 or over. 
     <br /> 
     <button id="over">I am 21 or over</button>&nbsp;&nbsp;&nbsp; 
     <button id="under">Under 21</button> 
    </div> 
    It's dusty under here! Let me be seen! 
</div> 

的JS是:

$('.body').block({ 
    message: $('#message'), 
    centerX: true, 
    centerY: true, 
    css: { 
     width: '600px', 
     height: '300px', 
     border: '3px solid #FF9900', 
     backgroundColor: '#000', 
     color: '#fff', 
     padding: '25px' 
    } 
}); 
$('#over').click(function() { 
    alert('clicked!'); 
    $('.body').unblock(); 
    return false; 
}); 
$('#under').click(function() { 
    $('.body').unblock(); 
    return false; 
}); 

見工作示例:http://jsfiddle.net/amyamy86/Taw83/

瞭解更多關於元阻斷這裏:http://www.malsup.com/jquery/block/#element

+0

我元素堵塞的實際初始彈出,但沒有想到以同樣的方式解除封鎖。咄!謝謝! – Eric 2013-03-26 20:46:30