在网上找到位大神 用预加载方式写的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 //缓存其他页面
});
}
1 个回复
CJH - 我想要的,我自然会认真
因为找不到对应的webview。id指的是Webview窗口标识,这个是在创建webview的时候生成的,如果传入无效的字符串则使用url参数作为WebviewObject窗口的id值。你源代码中根本就没有create,所有第一次肯定无法执行。
具体多看看webview部分的文档。
http://www.html5plus.org/doc/zh_cn/webview.html#plus.webview.create
http://www.html5plus.org/doc/zh_cn/webview.html#plus.webview.getWebviewById