简体   繁体   English

使用localStorage存储值

[英]store values with localStorage

I need some help here. 我需要一些帮助。 Whit this page i want to create own link tags with a image. 我想要用图像创建自己的链接标签。 When you click your image, you'll go to that page you have choose. 单击图像时,您将转到所选的页面。 I think i'm almost done but it does not work! 我想我快完成了,但是行不通! What have i done wrong?! 我做错了什么?! When I click the img element to open the new page, the image pops up.. 当我单击img元素以打开新页面时,图像弹出。

<head>
    <meta charset="utf-8">
    <title>Min Startsida</title>
    <script type="text/javascript">

        function newLink() {
            var myNewLink = document.getElementById("link");
                localStorage.setItem(link, myNewLink.value)
        };
        function newIcon() {
            var myNewIcon = document.getElementById("icon");
                localStorage.setItem(icon, myNewIcon.value)
        };
            function varIcon() {
                document.getElementById("image").src = localStorage.getItem(icon)
      };
    </script>
</head>
<body>
    <form>
        <h1>lägg till länk</h1><br />
        <input type="text" id="link"><br />
        <input type="text" id="icon"><br />
        <button onClick="newLink(), newIcon()">lägg till länk</button>
    </form>
    <section>
        <a href="#" onClick="location.href = localStorage.getItem(link)"><img src="#" id="image" onLoad="varIcon()"></a>
    </section>
</body>

This must be a string: 这必须是一个字符串:

localStorage.setItem("link", myNewLink.value)
                      ^^^^

what happens here is that the element is used as a key as link is used as an id - key has to be a string. 这里发生的是该元素用作键,而链接用作id-键必须是字符串。 This goes for both the setItem methods and the getItem as well that you use later: setItem方法和getItem都可以使用,以后再使用:

<a href="#" onClick="location.href = localStorage.getItem('link')">

Alos, this must be separated with a semi-column: 阿洛斯,这必须用半列分开:

 <button onClick="newLink(); newIcon()">
                           ^

(I didn't look further than these points) (我没有比这些要点更进一步)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM