小编joh*_*ton的帖子

localstorage 值在页面刷新时更改

我正在为我的 html 应用程序创建一个欢迎屏幕。我正在使用来自 github 的welcomescreen 插件。你可以在这里查看https://github.com/valnub/welcomescreen.js

现在我想在 localstorage 值为 0 时显示欢迎屏幕。当点击welcomescreen 的关闭按钮时,我将 localstorage 值更改为 1。但在页面刷新时,localstorage 值再次设置为 0。

如何做到这一点,这是我的 js 文件。

/*jslint browser: true*/
/*global console, Welcomescreen, $*/

// Init method

$(document).ready(function () {
  localStorage.setItem("welscreen", "0");	
  var welcomeTour = localStorage.getItem("welscreen");
	
  if (welcomeTour == 0) {

    $(document).ready(function () {     
       var options = {
            'bgcolor': '#0da6ec',
            'fontcolor': '#fff',
            'onOpened': function () {
             console.log("welcome screen opened");
	         console.log(welcomeTour);
       },
      'onClosed': function () {
          localStorage.setItem("welscreen","1");
	      var welcomeTour = localStorage.getItem("welscreen");
	      console.log("welcome screen closed");
	      console.log(welcomeTour);
        }
     }, …
Run Code Online (Sandbox Code Playgroud)

html javascript jquery local-storage

2
推荐指数
1
解决办法
1745
查看次数

如何将项目添加到本地存储

我正在使用"添加到收藏夹"按钮创建一个歌曲书应用程序.我有song1.html song2.html和favorite.html.

在song1.html中,单击添加到收藏夹按钮.我正在本地存储中存储该歌曲的链接.

这是我的song1.html

<!DOCTYPE html>
<html>
<body>



<button onclick="mySongOne()">add to favorite</button>





<script>
function mySongOne() {
  localStorage.setItem("favsong", "<a href='https://www.song1.com'><h1>song1</h1></a>");
}


</script>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

在song2.html中,单击添加到收藏夹按钮时.我将第二首歌曲的链接存储在本地存储中.

song2.html

<!DOCTYPE html>
<html>
<body>



<button onclick="mySongTwo()">add to favorite</button>



<script>
function mySongTwo() {
  localStorage.setItem("favsong", "<a href='https://song2.com'><h1>song2</h1></a>");
}


</script>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

现在我有一个favorite.html用于列出我最喜欢的歌曲.和favourite.html将检索我存储在本地存储中的链接.

favorite.html

<!DOCTYPE html>
<html>
<body onload="myFunction()">

<div id="result"></div>



<script>
function myFunction() {
  document.getElementById("result").innerHTML = localStorage.getItem("favsong");
}

</script>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

现在我想在favorite.html中显示歌曲1和歌曲2.但是只有歌曲2显示在favourite.html中.如何做到这一点.

html javascript local-storage

1
推荐指数
1
解决办法
3593
查看次数

标签 统计

html ×2

javascript ×2

local-storage ×2

jquery ×1