亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Bootstrap 模態(tài)對(duì)話框只加載一次 remote 數(shù)據(jù)的完美解決辦法

 更新時(shí)間:2017年07月09日 16:25:29   作者:dodo‘s  
前端框架 Bootstrap 的模態(tài)對(duì)話框,可以使用 remote 選項(xiàng)指定一個(gè) URL,這樣對(duì)話框在第一次彈出的時(shí)候就會(huì)自動(dòng)從這個(gè)地址加載數(shù)據(jù)到 .modal-body 中,但是它只會(huì)加載一次,不過通過在事件中調(diào)用 removeData() 方法可以解決這個(gè)問題,具體操作方法,大家通過本文了解下吧

摘要: 前端框架 Bootstrap 的模態(tài)對(duì)話框,可以使用 remote 選項(xiàng)指定一個(gè) URL,這樣對(duì)話框在第一次彈出的時(shí)候就會(huì)自動(dòng)從這個(gè)地址加載數(shù)據(jù)到 .modal-body 中,但是它只會(huì)加載一次,不過通過在事件中調(diào)用 removeData() 方法可以解決這個(gè)問題。

1. Bootstrap 模態(tài)對(duì)話框和簡(jiǎn)單使用

<div id="myModal" class="modal hide fade">
 <div class="modal-header">
  <button type="button" class="close" data-dismiss="modal">x</button>
  <h3>對(duì)話框標(biāo)題</h3>
 </div>
 <div class="modal-body">
  <p>對(duì)話框主體</p>
 </div>
 <div class="modal-footer">
  <a href="#" rel="external nofollow" rel="external nofollow" class="btn" data-dismiss="modal">取消</a>
  <a href="#" rel="external nofollow" rel="external nofollow" class="btn btn-primary" data-dismiss="modal">確定</a>
 </div>
</div>

顯示效果與下圖相似:

可以使用按鈕或鏈接直接調(diào)用模態(tài)對(duì)話框,這是簡(jiǎn)單的用法:

<button type="button" data-toggle="modal" data-target="#myModal">打開對(duì)話框</button>
<a href="#myModal" rel="external nofollow" role="button" class="btn" data-toggle="modal">打開對(duì)話框</button>

這樣只能把靜態(tài)內(nèi)容在對(duì)話框中顯示出來,使用對(duì)話框的 remote 選項(xiàng)可以實(shí)現(xiàn)更強(qiáng)大的效果。

2. 使用 remote 選項(xiàng)讓模態(tài)對(duì)話框加載頁面到 .modal-body 中

有兩種方法,一種是使用鏈接,另一種就是使用腳本。

2.1 使用鏈接

<a href="page.jsp" rel="external nofollow" data-toggle="modal" data-target="#myModal">打開對(duì)話框</a>

當(dāng)點(diǎn)擊此鏈接時(shí),page.jsp 的內(nèi)容會(huì)被加載到對(duì)話框的 .modal-body 中,隨即顯示對(duì)話框。

2.2 使用腳本

$("#myModal").modal({
 remote: "page.jsp"
});

這段腳本的效果和使用鏈接是一樣的,當(dāng)這段腳本執(zhí)行后,page.jsp 的內(nèi)容會(huì)被加載到對(duì)話框的 .modal-body 中,隨即顯示對(duì)話框。

這兩種方法的背后,都是 Bootstrap 調(diào)用了 jQuery 的 load() 方法,從服務(wù)器端加載了 page.jsp 頁面。但這個(gè)加載只會(huì)發(fā)生一次,后面不管你點(diǎn)擊幾次鏈接,或者執(zhí)行幾次腳本,哪怕改變傳遞給 remote 選項(xiàng)的值,對(duì)話框都不會(huì)重新加載頁面,這真是個(gè)讓人頭疼的事情。不過問題還是能夠解決的。

3. 移除數(shù)據(jù),讓對(duì)話框能夠在每次打開時(shí)重新加載頁面

在搜索并查閱了相關(guān)文檔后,發(fā)現(xiàn)在對(duì)話框的 hidden 事件里寫上一條語句就可以了:

$("#myModal").on("hidden", function() {
 $(this).removeData("modal");
});

也可以在每次打開對(duì)話框之前移除數(shù)據(jù),效果是一樣的。

注:上面的代碼基于 Bootstrap v2,如果使用 Bootstrape v3,模態(tài)對(duì)話框的 HTML 和事件的寫法有一些不同,例如對(duì)于上面的 hidden 事件,要寫成:

$("#myModal").on("hidden.bs.modal", function() {
 $(this).removeData("bs.modal");
});

以上所述是小編給大家介紹的Bootstrap 模態(tài)對(duì)話框只加載一次 remote 數(shù)據(jù)的完美解決辦法,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

最新評(píng)論