• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

HTML 5 web-storage localStorage

main

Neues Mitglied
hallo zusammen
ich möchte einen wert per localStorage speichern und diesen dann als class in meinem body tag ausgeben, via button.
kann mir jem. helfen?

gruß main
 
Willkommen im Forum.

- Informationen dazu zum Beispiel hier: Local Storage - Dive Into HTML5

Hier ein Ansatz:

HTML:
<!DOCTYPE html>

<html lang="en">

    <head>
        <meta charset="utf-8" />
        <title>Local storage demo</title>
        <style type="text/css">

            .myclass { background-color: #fcc; }

        </style>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <script type="text/javascript">

        $(document).ready(function () {

            (function bindEvents()
            {
                $('#demo').click(function () {
                    localStorage.setItem('bodyClass', 'myclass');
                    location.reload();
                });

                $('#reset').click(function () {
                    localStorage.clear();
                    location.reload();
                })
            })();

            var myClass = localStorage.getItem('bodyClass');
            if (myClass !== null) {
                $('body').addClass(myClass);
            }
        });

        </script>
    </head>

    <body>

        <button id="demo">Store class</button>
        <button id="reset">Reset</button>
    </body>

</html>
 
Zurück
Oben