逐个获取元素不起作用

时间:2014-02-16 22:09:41

标签: javascript key storage

嗨我有getelementbyid,我想在课堂上改变它但是不工作你能帮助我吗? 如果可能的话,我想将storageKey和storageData更改为getelementbyclass。

Javascript代码

    function addStorage() {
            var key = document.getElementById('storageKey');
            var data = document.getElementById('storageData');

            //localStorage setItem
            if ("localStorage" in window) {
                localStorage.setItem(key.value, data.value);
                location.reload();
            } else {
                alert("no localStorage in window");
            }

            //sessionStorage setItem
            if ("sessionStorage" in window) {
                sessionStorage.setItem(key.value, data.value);
                location.reload();
            } else {
                alert("no sessionStorage in window");
            }
        }

        function removeStorage() {
            var key = document.getElementById('removeKey');

            //localStorage removeItem
            if ("localStorage" in window) {
                if (localStorage.length > 0) {
                    localStorage.removeItem(key.value);
                    location.reload();
                }
            } else {
                alert("no localStorage in window");
            }

            //sessionStorage removeItem
            if ("sessionStorage" in window) {
                if (sessionStorage.length > 0) {
                    sessionStorage.removeItem(key.value);
                    location.reload();
                }
            } else {
                alert("no sessionStorage in window");
            }
        }

        function clearStorage() {
            //localStorage clear
            if ("localStorage" in window) {
                if (localStorage.length > 0) {
                    localStorage.clear();
                    location.reload();
                }
            } else {
                alert("no localStorage in window");
            }

            //sessionStorage clear
            if ("sessionStorage" in window) {
                if (sessionStorage.length > 0) {
                    sessionStorage.clear();
                    location.reload();
                }
            } else {
                alert("no sessionStorage in window");
            }
        }

        window.onload = function () {
            var localhtml = "";
            var sessionhtml = "";

            //localStorage key and getItem
            for (var i = 0; i < localStorage.length; i++) {
                localhtml += "<li>" + localStorage.key(i) + " : " + localStorage.getItem(localStorage.key(i)) + "</li>";
            }
            document.getElementById('localStorageData').innerHTML = localhtml;

            //sessionStorage key and getItem
            for (var j = 0; j < sessionStorage.length; j++) {
                sessionhtml += "<li>" + sessionStorage.key(j) + " : " + sessionStorage.getItem(sessionStorage.key(j)) + 

"</li>";
            }
            document.getElementById('sessionStorageData').innerHTML = sessionhtml;
        }

这是HTML代码

    Storage key :
<input type="text" id="storageKey"  style="width: 50px"  >
value :
<input type="text" id="storageData" style="width: 50px" >

1 个答案:

答案 0 :(得分:0)

我相信您的意思是使用document.getElementsByClassName - 尝试一下。