Javascript 使用jquery动态创建html页面
我会尽可能把这篇文章写得最短 我正在尝试创建一个jquery移动网页,在这里,我使用facebook javascript sdk从facebook页面导入照片和相册,并将它们显示到我的页面。我还使用该插件,通过旋转木马效果等,让相册很好地显示出来。您不需要了解插件或sdk来提供帮助,因为我可以准确地告诉您我的问题出在哪里,您只需要了解javascript/jquery 我的问题是,我确实导入了相册并将其显示给用户,但是我无法获得插件的旋转木马效果。根据插件的示例代码,当用户选择图片时,将执行以下代码:Javascript 使用jquery动态创建html页面,javascript,jquery,html,jquery-mobile,Javascript,Jquery,Html,Jquery Mobile,我会尽可能把这篇文章写得最短 我正在尝试创建一个jquery移动网页,在这里,我使用facebook javascript sdk从facebook页面导入照片和相册,并将它们显示到我的页面。我还使用该插件,通过旋转木马效果等,让相册很好地显示出来。您不需要了解插件或sdk来提供帮助,因为我可以准确地告诉您我的问题出在哪里,您只需要了解javascript/jquery 我的问题是,我确实导入了相册并将其显示给用户,但是我无法获得插件的旋转木马效果。根据插件的示例代码,当用户选择图片时,将执行以
$(document).ready(function(){
$('div.gallery-page')
.live('pageshow', function(e){
var
currentPage = $(e.target),
options = {},
photoSwipeInstance = $("ul.gallery a", e.target).photoSwipe(options, currentPage.attr('id'));
return true;
})
.live('pagehide', function(e){
var
currentPage = $(e.target),
photoSwipeInstance = PhotoSwipe.getInstance(currentPage.attr('id'));
if (typeof photoSwipeInstance != "undefined" && photoSwipeInstance != null) {
PhotoSwipe.detatch(photoSwipeInstance);
}
return true;
});
});
}(window, window.jQuery, window.Code.PhotoSwipe));
我对我的“动态生成”网页使用了相同的代码(我在代码中使用了.on),但似乎从未执行$('div.gallery-page')
中的代码。我真的不明白为什么,因为我生成的页面与示例页面完全相同。我给出了相同的类
名称,我使用了相同的div
一切
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CityInfo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<link href="photoSwipe/jquery-mobile.css" type="text/css" rel="stylesheet" />
<link href="photoSwipe/photoswipe.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="photoSwipe/klass.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
<script type="text/javascript" src="photoSwipe/code.photoswipe.jquery-3.0.5.min.js"></script>
<script type="text/javascript">
//PhotoSwipe
/*
* IMPORTANT!!!
* REMEMBER TO ADD rel="external" to your anchor tags.
* If you don't this will mess with how jQuery Mobile works
*/
(function(window, $, PhotoSwipe){
$(document).ready(function(){
$('div.gallery-page')
.on('pageshow', function(e){
var
currentPage = $(e.target),
options = {},
photoSwipeInstance = $("ul.gallery a", e.target).photoSwipe(options, currentPage.attr('id'));
return true;
})
.on('pagehide', function(e){
var
currentPage = $(e.target),
photoSwipeInstance = PhotoSwipe.getInstance(currentPage.attr('id'));
if (typeof photoSwipeInstance != "undefined" && photoSwipeInstance != null) {
PhotoSwipe.detatch(photoSwipeInstance);
}
return true;
});
});
}(window, window.jQuery, window.Code.PhotoSwipe));
</script>
</head>
<body>
<div id="fb-root"></div>
<script>
var albumPhotos = new Array();
var albumThumbnails = new Array();
// start the entire process
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : '564984346887426', // App ID from the app dashboard
channelUrl : 'channel.html', // Channel file for x-domain comms
status : true, // Check Facebook Login status
xfbml : true // Look for social plugins on the page
});
FB.api('169070991963/albums', checkForErrorFirst(getAlbums));
}
// checkForErrorFirst wraps your function around the error checking code first
// if there is no response, then your code will not be called
// this allows you to just write the juicy working code
// and not worry about error checking
function checkForErrorFirst(myFunc) {
return function(response) {
if (!response || response.error) {
alert("Noo!!");
} else {
myFunc(response);
}
};
}
function getAlbums(response) {
for (var i=0; i < response.data.length; ++i) {
processAlbum(response.data[i], i);
}
}
function processAlbum(album, i) {
FB.api(album.id + "/photos", checkForErrorFirst(populateAlbum(album, i)));
}
function populateAlbum(album, i) {
return function(response) {
for (var k=0; k < response.data.length; ++k){
albumThumbnails[i] = albumThumbnails[i]||[];
albumThumbnails[i][k] = response.data[k].picture;
albumPhotos[i] = albumPhotos[i]||[];
albumPhotos[i][k] = response.data[k].source;
}
// now that we've populated the album thumbnails and photos, we can render the album
FB.api(album.cover_photo, checkForErrorFirst(renderAlbum(album, i)));
};
}
function renderAlbum(album, i) {
return function(response) {
var albumName = album.name;
var albumCover = album.cover_photo;
var albumId = album.id;
var numberOfPhotos = album.count;
// render photos
$(".albums").append('<li>'+
'<a href="#Gallery' + i + '"' + 'data-transition="slidedown">'+
'<img src= "' + response.picture + '" />'+
'<h2>' + albumName + '</h2>'+
'<p>' + "Number of Photos: " + numberOfPhotos +'</p>'+
'</a>'+
'</li>').listview('refresh');
$("#Home").after('<div data-role="page" data-add-back-btn="true" id=Gallery'+ i +
' class="gallery-page"> ' +
' <div data-role="header"><h1>Gallery</h1></div> ' + ' <div data-role="content"> ' +
' <ul class="gallery"></ul> ' + ' </div> ' +
' </div> ');
for(var x=0; x < albumPhotos[i].length; x++)
$('#Gallery' + i + ' .gallery').append('<li><a href="' + albumPhotos[i][x]
+ '" rel="external"><img src="' + albumThumbnails[i][x] + '"' + '/> </a> </li>');
};
}
// Load the SDK asynchronously
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div data-role="page" id="Home" data-theme="c">
<div data-role="content">
<h2 id="banner" align = "center">Photo Albums</h2>
<ul data-role="listview" data-inset="true" class="albums">
</ul>
</div>
</div>
</body>
</html>
城市信息
//擦照片
/*
*重要的!!!
*记住将rel=“external”添加到锚定标记中。
*如果不这样做,这将影响jQuery Mobile的工作方式
*/
(功能(窗口,$,Photosweep){
$(文档).ready(函数(){
$('div.gallery-page')
.on('pageshow',函数(e){
变量
当前页面=$(e.target),
选项={},
PhotosweepInstance=$(“ul.gallery a”,e.target).Photosweep(选项,currentPage.attr('id'));
返回true;
})
.on('pagehide',函数(e){
变量
当前页面=$(e.target),
PhotosweepInstance=Photosweep.getInstance(currentPage.attr('id');
if(类型为PhotosweepInstance!=“未定义”&&PhotosweepInstance!=null){
Photosweep.depatch(Photosweep实例);
}
返回true;
});
});
}(window,window.jQuery,window.Code.photoswip));
var albumPhotos=新数组();
var albumThumbnails=新数组();
//开始整个过程
window.fbAsyninit=函数(){
//初始化FBJSSDK
FB.init({
appId:'564984346887426',//应用程序仪表板中的应用程序ID
channelUrl:'channel.html',//用于x域通信的通道文件
状态:true,//检查Facebook登录状态
xfbml:true//在页面上查找社交插件
});
FB.api('169070991963/相册',checkForErrorFirst(getAlbums));
}
//checkForErrorFirst首先围绕错误检查代码包装函数
//如果没有响应,则不会调用代码
//这允许您只编写有趣的工作代码
//不用担心错误检查
函数checkForErrorFirst(myFunc){
返回函数(响应){
如果(!response | | response.error){
警惕(“Noo!!”);
}否则{
myFunc(答复);
}
};
}
函数getAlbums(响应){
对于(变量i=0;i”+
''+
“”)。列表视图(“刷新”);
美元(“#家”)。之后(“”+
“画廊”+“+
“
”+”+
' ');
对于(var x=0;x<1[i]。长度;x++)
$(“#Gallery'+i+'.Gallery')。追加(“”);
};
}
//异步加载SDK
(功能(d、s、id){
var js,fjs=d.getElementsByTagName[0];
if(d.getElementById(id)){return;}
js=d.createElement;js.id=id;
js.src=“//connect.facebook.net/en_US/all.js”;
fjs.parentNode.insertBefore(js,fjs);
}(文档“脚本”、“facebook jssdk”);
相册
$("#Home").after('<div data-role="page" data-add-back-btn="true" id=Gallery'+ i +
' class="gallery-page"> ' +
' <div data-role="header"><h1>Gallery</h1></div> ' + ' <div data-role="content"> ' +
' <ul class="gallery"></ul> ' + ' </div> ' +
' </div> ');
jquery-1.9.1.min.js
$(document)
.on('pageshow', 'div.gallery-page', function(e){
var
currentPage = $(e.target),
options = {},
photoSwipeInstance = $("ul.gallery a", e.target).photoSwipe(options, currentPage.attr('id'));
return true;
})
.on('pagehide', 'div.gallery-page', function(e){
var
currentPage = $(e.target),
photoSwipeInstance = PhotoSwipe.getInstance(currentPage.attr('id'));
if (typeof photoSwipeInstance != "undefined" && photoSwipeInstance != null) {
PhotoSwipe.detatch(photoSwipeInstance);
}
return true;
});
});
$(document).on('event', '.element', function () { magic });