0

localStorage に格納された値を変換してからクラスに変換して、DOM で操作できるようにしようとしています。

私はJavaScriptに非常に慣れていないので、説明させてください:

複数の div を含む html ファイルがあり、localStorage は最後にクリックされた div のクラスを格納します。

たとえば、スクリプトで localStorage から保存されたクラスを呼び出し、そのクラスで div を見つけて (jquery OR js を使用しても問題ありません)、.css() を使用してその div の背景色を変更します。そのロジックで必要なことを実行できますが、機能させることができません。

だから私がやろうとしているのは $('the last clicked div').css({..manipulate the css..}); です。

これは可能ですか?

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>test</title>
    <script type="text/javascript">

    $(document).ready(function () {

        //always show the current div class
        $("b").html(localStorage.getItem("currentDiv"));


        //get the class of the div that's just been clicked
        $("div").click(function(){
            var currentClass = $(this).attr("class");
            localStorage.setItem("currentDiv", currentClass);
            $("b").html(localStorage.getItem("currentDiv"));
        });


        //show the div that was last clicked
        function currentStatus(){
            if (localStorage.getItem("currentDiv") === $(currentClass))
            {
                $(currentClass).show();
                $("b").html(localStorage.getItem("currentDiv"));
            }
        }

        //set a color for the recently clicked div dynamically, not by .click
        var highlightClass = localStorage.getItem("currentDiv");
        highlightClass.css({
            'background' : 'black'
        })

    });

    $('#localStorageTest').submit(function() {
        localStorage.clear();
    });


    </script>

    <style type="text/css">
    [class*="slide"]{
    display: inline-block;
    padding: 40px;
    background: #999;
    margin: 20px;
    }
    /*.slide1{
    display: block;
    }*/
    </style>

    </head>

    <body onLoad="currentStatus()">
        <div class="slide1">
            <h1>"A question would go here."</h1>
        </div>
        <div class="slide2">
            <h1>"A question would go here."</h1>
        </div>
        <div class="slide3">
            <h1>"A question would go here."</h1>
        </div>
        <div class="slide4">
            <h1>"A question would go here."</h1>
        </div>
        <div class="slide5">
            <h1>"A question would go here."</h1>
        </div>
        <div class="slide6">
            <h1>"A question would go here."</h1>
        </div>
        <div class="slide7">
            <h1>"A question would go here."</h1>
        </div>
        <b></b>
    </body>
    </html>
4

1 に答える 1