jquery动态加载页面方案

资源来源网上,做个备忘

1、分别加载的html及js,分成两个文件
jquery加载页面
$('#区域id').load('页面名称');
JQuery的
$.getScript(url,callback) 方法加载js脚本

js加载例子参照
[url]
http://www.cnblogs.com/58top/archive/2012/10/29/loading-javascript-dynamically-using.html
[/url]

获取url参数所指定的脚本,使用一个GET请求到指定的服务器

Parameters
URL()获取脚本文件的URL。

callback (Function)可选函数 调用脚本文件加载后里面的函数

几种不同的js加载方法
[url]
http://www.jb51.net/article/17992.htm
[/url]

2、js加载使用requireJS
具体例子参照
[url]
http://www.cnblogs.com/snandy/archive/2012/05/22/2513652.html
[/url]

页面调用

<script data-main="main" src="require.js"></script>


main.js

require.config({
paths: {
jquery: 'jquery-1.7.2'
}
});

require(['jquery'], function($) {
alert($().jquery);
});


3、html5 定义data-*属性自定义加载
效果参考
[url]
http://wbpreview.com/previews/WB095F5T9/
[/url]


<sideBar><ul><li data-link="features"><figure>Features</figure></li></ul></sideBar>



$(".sideBar>ul>li>figure:first-child").on("click",function(){
var link = $(this).parent().attr('data-link');
if(link != 'none'){
loadPage(link.toString().toLowerCase());
}
if(!$(this).parent().find('.subSide').length){
$('.subSide').slideUp();
}
});

function loadPage(page){
$.ajax({
url:"pages/"+page.toString()+".html",
cache:false,
success: function(data){
$(".content").empty();
$(".content").html(data);
$(".content").hide().fadeIn();
updateSidebar(page);
updateBreadCrumbs(); //Update the BreadCrumbs
notification("You're in "+ page,"blue",2000);
}
}).done(function(){
//This is the execution chain for different pages.
//Note: Sub Pages are also defined here
//Functions used in this switch statement are defined in a new object named "chain"
//this object contains all functions that you want to load when a specific page is initialized
switch(page){
case 'dashboard': chain.dashboard();
break;
case 'home' : chain.home();
break;
...
default: console.log('Page loading chain not identified, Add a new chain in main.js under chain module.');
}
});
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值