witt
witt
  • 发布:2017-03-27 11:33
  • 更新:2017-03-28 11:38
  • 阅读:2003

mui用预加载方式写的navbar的问题

分类:MUI

在网上找到位大神 用预加载方式写的navbar

先显示首页,然后预加载其他的页面

为什么首次加载页面不能点击

再次刷新却能点击了呢?

以下是代码:

首次运行就会报错:Uncaught TypeError: Cannot read property 'hide' of null

怀疑是mui.plusReady的问题,但是不会解决了

var hrefArr = [  
    { "title": "首页", "id": "index", "href": "index.html", "icon": "icon-icon" },  
    { "title": "口碑", "id": "praise", "href": "praise.html", "icon": "icon-praise" },  
    { "title": "朋友", "id": "friends", "href": "friends.html", "icon": "icon-friend" },  
    { "title": "我的", "id": "my", "href": "my.html", "icon": "icon-my" }  
]; //要跳转的  页面跟他的ID要先写好     
var navFirst = [], // 第一个页面数据   
    navOther = [], // 其他页面数据  
    viewObj = [] // 窗体对象    

mui.plusReady(function() {  
    navInit();  
    console.log(JSON.stringify(navFirst));  
    clickFun(navFirst[0].id, plus.webview.getWebviewById(navFirst[0].id));  
    console.log(navFirst[0].id);  
    mui("#nav").on("tap", "a", function(e) {  
        var id = $(this).attr("id");  
        var web = plus.webview.getWebviewById(id);  
        clickFun(id, web);  
    })  
});  

function clickFun(id, web) {  
    if(id == navFirst[0].id) {  
        $.each(navOther, function(i, v) {  
            var otherObj = plus.webview.getWebviewById(v.id);  
            otherObj.hide();  
        })  
    };  
    web.show();  
}  
//初始化html    
function navInit() {  
    var html = "";  
    $.each(hrefArr, function(i, v) {  
        if(i == 0) {  
            html += '<a class="mui-tab-item' + ((i == 0) ? " mui-active" : "") + ' " id="' + v.id + '">' +  
                '<span class="mui-icon iconfont ' + v.icon + '"></span>' +  
                '<span class="mui-tab-label">' + v.title + '</span>' +  
                '</a>'  
        } else {  
            html += '<a class="mui-tab-item' + ((i == 0) ? " mui-active" : "") + ' " id="' + v.id + '">' +  
                '<span class="mui-icon iconfont ' + v.icon + '"></span>' +  
                '<span class="mui-tab-label">' + v.title + '</span>' +  
                '</a>'  
        }  

    });  
    $("#nav").html(html);  
    $.each(hrefArr, function(i, v) {  
        var arrI = { "url": v.href, "id": v.id, styles: { top: '0', bottom: '51px' } };  
        console.log(arrI);  
        if(i == 0) { //第一个页面    
            navFirst.push(arrI)  
            console.log(JSON.stringify(navFirst));  
        } else { //缓存住的页面    
            navOther.push(arrI)  
            console.log(JSON.stringify(navOther));  
        }  
    });  
    mui.init({  
        subpages: navFirst, //先加载首页    
        preloadPages: navOther //缓存其他页面    
    });  
}
2017-03-27 11:33 负责人:无 分享
已邀请:
CJH

CJH - 我想要的,我自然会认真

该问题目前已经被锁定, 无法添加新回复