JQuery Mobile navbar Dynamic Refresh creation

Source: Internet
Author: User

Today, we suddenly use the need to dynamically change the tab page,

The layout code is as follows:

<div data-role= "NavBar" id= ' Divtab ' >  <ul id= ' Divtabul ' >         </ul></div>
Create the JS code dynamically as follows:

function Createtabs (tabs) {     var html = ';     for (Var i=0;i<tabs.length;i++) {      html = html + createtab (tabs[i].tabname,tabs[i].tabid,tabs[i].ispage);     }     $ (' #divtabul '). empty ();     $ (' #divtabul '). HTML (HTML);     $ (' #divtab '). NavBar (' refresh ');//Note this line, Danielinbiti   }   function Createtab (tabname,tabid,ispage) {     var lihtml = ' <li><a onclick= ' Clicktab (this,true) "  data-ajax=" false "txt=" ' +tabname            + ' "id=" ' + TabID + ' "ispage=" ' + ispage + ' "class=" Self-class "> ' + tabname + ' </a></li> ';     return lihtml;   }


began to think NavBar refresh and ListView basically similar, the results found NavBar no refresh, only _create, and create is not external use.

Looking for a circle on the internet, there seems to be no useful, direct navbar () way, layout is problematic.

So I add navbar, the following code is saved as Jquery-mobile-navbar.js

$.widget ("Mobile.navbar", $.mobile.widget, {options: {iconpos: "Top", Grid:null,initselector: ": Jqmdata (role= ') NavBar ') "},_create:function () {var t=this; T.refresh ();},refresh:function () {var $navbar = this.element, $navbtns = $n Avbar.find ("a"), Iconpos = $navbtns. Filter (": Jqmdata (Icon)"). Length? this.options.iconpos:undefined;$ Navbar.addclass ("Ui-navbar Ui-mini"). attr ("Role", "navigation"). Find ("ul"). Jqmenhanceable (). Grid ({grid: This.options.grid}); $navbtns. Buttonmarkup ({corners:false,shadow:false,inline:true,iconpos:iconpos}); $ Navbar.delegate ("A", "Vclick", function (event) {if (!$ (event.target). Hasclass ("ui-disabled")) {$navbtns. removeclass ($.mobile.activebtnclass); $ (this). addclass ($.mobile.activebtnclass);}); /Buttons in the NavBar with Ui-state-persist class should regain their active state before page Show$navbar.closest (". UI -page "). Bind (" Pagebeforeshow ", function () {$navbtns. Filter (". Ui-state-persist "). AddClass ($.mobile.activebtnclass );});}}); 

Introduce JS in the page as follows

<script src= ". /js/sys/jquery.mobile-1.2.0.min.js "></script><script src=". /js/sys/jquery-mobile-navbar.js "></script>

After the above treatment, you can use

$ (' #divtab '). NavBar (' refresh ');
is refreshed.

Dynamic results are as follows, Test tab refresh

function Testtabs () {     var list = new Array;     for (Var i=0;i<3;i++) {     var obj = new Object;     Obj.tabname= ' tab ' +i;     obj.tabid= ' tab ' +i;     obj.ispage=i;     List[list.length] = obj;     }     Createtabs (list);   }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.