如何从会话存储中获取值并在JavaScript的处理函数中使用它

时间:2019-10-12 13:01:18

标签: javascript session-storage

无法使用if条件将会话存储设置项的值传递到ready函数中以执行其他操作:

    <script type="text/javascript">
        function Re() {
            sessionStorage.setItem("hasClick", true);
            window.location.replace("http://127.0.0.1:5000/test");
        }

        function ready(callback) {

            if (document.readyState != 'loading') callback();

            else if (document.addEventListener) document.addEventListener('DOMContentLoaded', callback);

            else document.attachEvent('onreadystatechange', function () {
                if (document.readyState == 'complete') callback();
            });
        }

        ready(function () {
            console.log("It worked!");
            var hasClick = localStorage.getItem("hasClick");
            if (hasClick == true) {
                var labels = document.getElementsByTagName('label');
                labels[4].style.cssText = 'color:blue; font-size:22px;';
            }
        });
    </script>

   var hasClick = localStorage.getItem("hasClick");

值未传递给hasClick

0 个答案:

没有答案