国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

使用Bootstrap Tabs選項(xiàng)卡Ajax加載數(shù)據(jù)實(shí)現(xiàn)

2019-11-19 18:17:58
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了Bootstrap Tabs選項(xiàng)卡Ajax加載數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下

HTML代碼(僅展示了部分關(guān)鍵性代碼)

<li class="active"> <a href="#home" data-toggle="tab" name="menu-ctrl">  <span class="glyphicon glyphicon-home"> </span> 主頁  <span class="sr-only">(current)</span>  </a></li><li> <a href="#test-paper">  <span class="glyphicon glyphicon-list-alt"> </span> 試卷庫 </a></li><li> <a href="#favorite">  <span class="glyphicon glyphicon-bookmark"> </span> 我的收藏 </a></li><li> <a href="#about"> <span class="glyphicon glyphicon-info-sign"> </span> 關(guān)于 </a></li> <!--- /. Nav tabs---><div id="myTabContent" class="tab-content"> <!-- 試卷庫頁面 --> <div class="tab-pane fade" id="test-paper"></div> <!-- 收藏頁面 --> <div class="tab-pane fade" id="favorite"></div> <!-- 關(guān)于頁面 --> <div class="tab-pane fade" id="about"></div> <!-- 用戶信息頁面 --> <div class="tab-pane fade" id="user-info-page"></div></div> <!--- /.tab-content ---->

實(shí)現(xiàn)思路:

1.使用JavaScript激活tab選項(xiàng)卡:

$("a[href=['#about']").click(function(e){ $(this).tab('show'); e.preventDefault(); //阻止a標(biāo)簽的默認(rèn)行為});

2.使用jQuery的load()方法異步加載 tab-pane容器中的內(nèi)容;

$('#about').load('pages/about.jsp');

大功告成!

那么接下來對上面的思路進(jìn)行簡單的封裝

JavaScript代碼:

/** * 激活tab選項(xiàng)卡并使用ajax異步加載內(nèi)容 * @param {Object} tabsId* @param {Object} url */function showTabs(tabsId,url) { $("a[href='#"+tabsId+"']").tab('show'); var $tabContent = $('#'+tabsId); if($tabContent.length < 100) { $tabContent.load(url); //console.info(tabsId + ' load done!'); }}//依次為每個(gè)tab導(dǎo)航a標(biāo)簽添加單擊事件$('a[href="#test-paper"]').click(function(e) { showTabs('test-paper','pages/test-paper.jsp'); e.preventDefault();});//$('a[href=..]')...//..//.. 這么長的代碼!!

考慮到每個(gè)a標(biāo)簽的綁定的都是click事件,只是參數(shù)不同而已, 可以嘗試著把tabs的數(shù)據(jù)用json數(shù)組存儲(chǔ)起來;

//準(zhǔn)備tabs數(shù)據(jù)var tabsData = [{ "id" : "test-paper", "url" : "pages/test-paper.jsp"},{ "id" : "favorite", "url" : "pages/favorite.jsp"},{ "id" : "about", "url" : "pages/about.jsp"},{ "id" : "user-info-page", "url" : "pages/user-info.jsp"}];//遍歷json數(shù)組,循環(huán)添加a標(biāo)簽click事件:$(tabsData).each(function(){ //console.info(this.id + "--->" + this.url); $("a[href='#"+this.id+"']").click(function(e) { showTabs(this.id,this.url); e.preventDefault(); });});

終于完成? No!實(shí)測運(yùn)行中沒有任何效果;這法子貌似行不通啊!原因暫時(shí)還在研究中(..)

這時(shí)我想到了jQuery的bind()函數(shù):

bind(type,[data],fn);

//fn: 綁定到每個(gè)匹配元素的事件上面的處理函數(shù)
//可以嘗試把每個(gè)tab的參數(shù)通過data傳遞到外部的function中,用作每個(gè)a標(biāo)簽的click響應(yīng)函數(shù)

改寫后的$.each()循環(huán):

$(tabsData).each(function(){ //console.info(this.id + "--->" + this.url); $("a[href='#"+this.id+"']").bind('click',{ id : this.id, url : this.url },tabsHandler);});function tabsHandler(event) { var data = event.data; showTabs(data.id,data.url); return false; //阻止默認(rèn)a標(biāo)簽響應(yīng)}

這次總算是成功了!

看下演示圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 寻乌县| 洛宁县| 蛟河市| 永兴县| 永福县| 永丰县| 察雅县| 酒泉市| 岳西县| 望都县| 房山区| 沙雅县| 白山市| 西乌珠穆沁旗| 筠连县| 揭东县| 靖宇县| 柳州市| 淄博市| 大名县| 樟树市| 开平市| 平定县| 自治县| 拜泉县| 汕头市| 朝阳区| 巴东县| 鄯善县| 扶余县| 崇明县| 韶关市| 日照市| 济源市| 海林市| 杂多县| 商水县| 高台县| 介休市| 哈密市| 贺州市|