在css載入完畢後執行後續代碼

來源:互聯網
上載者:User

標籤:style   blog   http   java   使用   io   strong   ar   art   

最近在寫項目的framework,寫個JQueryMessageBox的類,以使用jquery ui中的dialog()來顯示訊息框,為了使方法方便調用,便加入了自動判斷頁面是否加入了ui.js和ui.css,代碼如下:

[javascript] view plaincopyprint? 
  1. //如果沒有包含ui.js,則引用  
  2.     if ($(‘script[src$=""jquery-ui-1.8.11.custom.min.js""]‘).length == 0) {{  
  3.         $(""<script src=‘/js/jquery-ui-1.8.11.custom.min.js‘ type=‘text/javascript‘ />"").appendTo(‘head‘);  
  4.     }}  
  5. //如果沒有載入css,則載入  
  6. if ($(‘link[ref$=""jquery-ui-1.8.14.custom.css""]‘).length == 0) {{  
  7.         $(‘<link href=""/css/jquery-ui-1.8.14.custom.css"" rel=""stylesheet"" type=""text/css"" />‘).appendTo(‘head‘);  
  8.   
  9. //dialog() script  
  10.     }}  

但CSS代碼不會立即載入下來,於是在顯示dialog的時候則不會有樣式(在IE9下可以,因為在IE9下CSS即使後下載下來,也會重新繪製頁面元素,而IE8則不會).解決此問題的方法,可以使用ajax,當css載入完畢後,再顯示dialog,這樣就可以帶著樣式顯示出來了,代碼如下:

 

[javascript] view plaincopyprint? 
  1. if ($(‘link[ref$=""jquery-ui-1.8.14.custom.css""]‘).length == 0) {  
  2.                 $.ajax({  
  3.                     url: ‘/css/jquery-ui-1.8.14.custom.css‘,  
  4.                     success: function(data) {  
  5.                         //建立一個style元素,並追加到head中  
  6.                         //替換其中images的路徑  
  7.                         $(‘<style type="text/css">‘ + data.replace(/url\(images/g, ‘url(/css/images‘) + ‘</style>‘).appendTo(‘head‘);  
  8.                         //dialog() script;  
  9.                     }  
  10.                 });  
  11.             }  
  12.             else {   
  13.                 //dialog() script;  
  14.             }  

在css載入完畢後執行後續代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.