【发布时间】:2016-01-05 14:38:33
【问题描述】:
我必须归档的内容:
- 切换带有所选类别的图像
- 重新加载页面,之前选择的图片应该会被记住。
我遇到的问题: myPhotos 数组没有获取每个“选定”项目...它使用“选定”类循环元素,但每次都添加相同的元素。
第二个问题是 displayPhotos 函数需要更改为每次都显示所有图像。 但在刷新时保留选中的那些
请帮忙。
我创建了一个小提琴,以防下面的代码不起作用。 https://jsfiddle.net/1mes2z6s/1/
// Creates an empty array for selected photos to be added to
myPhotos = [];
// ---------------------------------------------
// Callback function to get photos from flickr
// ---------------------------------------------
(function() {
//changed the callback so that it is defined
window.cb = function(data) {
//user returned data
displayPhotos(data);
}
var tags = "london";
var script = document.createElement('script');
script.src = "https://api.flickr.com/services/feeds/photos_public.gne?format=json&jsoncallback=cb&tags=" + tags;
document.head.appendChild(script);
})();
// ------------------------------------
// Display photos
// ------------------------------------
function displayPhotos(data) {
var $images = $('#images');
// Check if localStorage key is empty (e.g. no photos have previously been selected) and go and get some
if (localStorage.getItem("mySavedPhotos") === null) {
// loop through photos in the JSON endpoint
for (var i = 0; i < data.items.length; i++) {
$('<img/>').attr('src', data.items[i].media.m).appendTo($images);
}
// localStorage key has items, so show them instead
} else {
// retrive the items in localStorage
var len = JSON.parse(localStorage['mySavedPhotos']).length
var item = JSON.parse(localStorage['mySavedPhotos'])
// loop through photos in local storage
for (var i = 0; i < len; i++) {
$('<img/>').attr('src', item[i]).appendTo($images);
}
}
}
// ------------------------------------
// Add 'selected' class to photo, push it to the 'myPhotos' array, then add array to localStorage
// ------------------------------------
function selectPhoto(photo) {
// add 'selected' class to image
photo.addClass('selected');
// push the 'src' of that image to the array
myPhotos.push(photo.attr('src'));
var mySavedPhotos = myPhotos;
// JSON stingify the items in local storage so they can be accessed easily
localStorage['mySavedPhotos'] = JSON.stringify(mySavedPhotos);
// debug
// console.log(JSON.parse(localStorage['mySavedPhotos']));
}
// ------------------------------------
// JQUERY DOM LOADED
// ------------------------------------
$(function() {
// When user clicks on photo to select
$('#images').on('click', 'img', function() {
selectPhoto($(this));
});
// Clear the local storage items (for debug)
$('#debugBtn').on('click', 'button', function() {
localStorage.removeItem("mySavedPhotos");
console.log(localStorage);
});
});
div#images img {
width: 100px;
height: 100px;
margin: 10px;
border: 3px solid #fff;
}
div#images img.selected {
border: 3px solid #f06;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- this is where the photos will be displayed -->
<div id="images"></div>
<!-- this is a button to clear localStorage items and is for debugging -->
<div id="debugBtn">
<button>Remove LocalStorage Key</button>
</div>
【问题讨论】:
-
为什么要重新加载页面?您不能对 fickr API 进行 AJAX 调用并相应地更新 UI 吗?
-
@AdamJeffers 我需要能够记住刷新后选择的图像
-
是的,我明白了...我只是想知道您是否需要费心“刷新”页面?你能描述一下场景吗?
-
@AdamJeffers 不难理解...如果用户返回此页面 OP 希望维护状态
-
@AdamJeffers 如果页面刷新,用户应该能够看到选定的图像。目前只有一次选择可见,我需要看到所有加上未选择的
标签: javascript jquery css json