刷新页面后为什么没有检索到Img?

时间:2015-11-26 10:50:46

标签: php jquery html css

我想在本地存储html5中存储多个动态图像,并在页面刷新时检索......

这是我的代码和jsfiddle

HTML

<ul class="lib">
    <li>
        <img class='my_image' src='http://wcdn1.dataknet.com/static/resources/icons/set95/5076c9d9.png'>
    </li>
    <li>
        <img class='my_image' src='http://cdn.mysitemyway.com/icons-watermarks/simple-black/raphael/raphael_gear-small/raphael_gear-small_simple-black_128x128.png'>
    </li>
</ul>Click the image to make clone and then click save btn to store in local sorage html5 and after that refresh to retreave the images in below box
<div class="block" id="block_test"></div>
<input id="button" type="button" value="save">

CSS

.lib {
    border:1px solid #000;
}
.lib li {
    display:inline;
}
.lib img {
    width:100px;
}
#block_test {
    border:1px solid #000;
    height:300px;
}

JS

$(".my_image").click(function () {
    $(this).clone().addClass('input').removeClass('my_image').appendTo(".block").freetrans({
        x: 0,
        y: 0
    });
});

function setBackground() {
    localStorage.input = $(".input").attr('src');
}

function loadBackground() {
    if (localStorage.input.length == 0) {
        localStorage.input = "empty";
    } else {
        var img_elem = $('<img id="output">');
        img_elem.attr('src', localStorage.input).addClass('input').removeClass('my_image').appendTo(".block").freetrans({
            x: 0,
            y: 0
        });

        localStorage.input = $(arr[0]).attr('src');
    }
}

$("#button").click(function () {
    setBackground();
});

链接到jsfiddle http://jsfiddle.net/fq58hrh1/14/

提前致谢。我希望我清楚我的问题

1 个答案:

答案 0 :(得分:2)

使用localStorage是代码中的错误,下面我解释一下

使用window.localStorage - 存储没有过期日期的数据 当您需要设置应在下一页或重新加载后反映的变量时,请使用:

var someVarName =“value”; localStorage.setItem(“someVarName”,someVarName);

在任何页面中(例如页面加载时),请将其命名为:

var getVarName = localStorage.getItem("someVarName");

.getItem()将返回null或存储的值。

您更新的javascript如下

$(".my_image").click(function () {     $(this).clone().addClass('input').removeClass('my_image').appendTo(".block").freetrans({
        x: 0,
        y: 0
    });
});

function setBackground() {
    localStorage.setItem("someVarName", $("#block_test").html());
}

function loadBackground() {
    if(localStorage.getItem("someVarName")!=null){
        var storedImages = localStorage.getItem("someVarName");
        $("#block_test").html(storedImages)
    } else {
        $("#block_test").html("No Images Yet");
    }
}

$("#button").click(function () {
    setBackground();
});

$(document).ready(function () {
    loadBackground();
});

我提供了一个更新的演示http://jsfiddle.net/fq58hrh1/29/要测试它,请单击图像,然后保存,然后重新加载页面。