2017-11-30 7 views
2

To-Doアイテムをすべてローカルストレージに保存し、コンピュータを再起動したりページを更新したりするときは、すべてのアイテムがそのページに表示されます。元の注文。?To-DoリストデータをHTMLでローカルストレージに保存する

//check of spec 
 
$("ol").on("click", "li", function(){ 
 
$(this).toggleClass("completed"); 
 

 
}); 
 

 
//click on X to delete To-DO 
 

 
$("ol").on("click", "span", function(event){ 
 
\t $(this).parent().fadeOut(500,function(){ 
 
\t \t $(this).remove(); 
 
\t }); 
 
\t event.stopPropagation(); 
 
}); 
 

 
$("input[type='text'").keypress(function(event){ 
 
\t if(event.which === 13) { 
 
\t \t //grabbing the text typed 
 
\t \t var todoText = $(this).val(); 
 
\t \t $(this).val(""); 
 
\t \t //create a new LI and add to UL 
 
\t \t $("ol").append("<li><span><i class='fa fa-trash'></i></span> " + todoText +"</li>") 
 
\t } 
 
}); 
 

 
$(".fa-plus").click(function(){ 
 
\t $("input[type='text'").fadeToggle(); 
 
});
h1 { 
 
\t background: #2980b9; 
 
\t color: white; 
 
\t margin: 0; 
 
\t padding: 10px 15px; 
 
\t text-transform: uppercase; 
 
\t font-size: 24px; 
 
\t font-weight: normal; 
 

 
} 
 

 
iframe { 
 
\t float: left; 
 
} 
 

 
ol { 
 
\t /* THE BULLET POINTS 
 
\t list-style: none; 
 
\t */ 
 
\t margin: 0; 
 
\t padding: 0; 
 
\t font-size: 18px; 
 

 
} 
 

 
body { 
 

 
background-color: rgb(13, 168, 108); 
 

 
} 
 

 
li { 
 
\t background: #fff; 
 
\t height: 30px; 
 
\t line-height: 30px; 
 
\t color: #666; 
 
} 
 

 
li:nth-child(2n){ 
 
\t background: #d3d3d3; 
 
} 
 

 
span { 
 

 
\t height: 30px; 
 
\t width: 0px; 
 
\t margin-right: 20px; 
 
\t text-align: center; 
 
\t color:white; 
 
\t display: inline-block; 
 
\t transition: 0.2s linear; 
 
\t opacity:0; 
 
\t background: #e74c3c 
 
\t 
 

 
} 
 

 
li:hover span { 
 
\t width: 30px; 
 
\t opacity: 1.0; 
 
} 
 

 
input { 
 
\t font-size: 18px; 
 
\t width: 100%; 
 
\t padding: 13px 13px 13px 20px; 
 
\t box-sizing: border-box; 
 
\t border: 3px solid rgba(0,0,0,0); 
 
\t color: #2980b9; 
 
\t background-color: #e4e4e4; 
 

 
} 
 

 
input:focus { 
 
\t background: white; 
 
\t border: 3px solid green; 
 

 
\t /*OUTLINE OF INPUT BOX 
 
\t outlin: none; */ 
 
} 
 

 
.fa-plus { 
 
\t float: right; 
 
} 
 

 

 
#container { 
 
\t 
 
\t width: 360px; 
 
\t margin: 60px auto; 
 
\t background: #d3d3d3; 
 
\t box-shadow: 0 0 3px rgba(0,0,0,0.1); 
 

 

 
} 
 

 
.completed { 
 
\t color: red; 
 
\t text-decoration: line-through; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title>ToDo List</title> 
 
\t <link rel="stylesheet" type="text/css" href="utd.css"> 
 
\t <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> 
 
\t <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css"> 
 

 
\t <script type="text/javascript" src="jquery-3.2.1.min.js"></script> 
 
\t <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 
\t <script> 
 
    $(function() { 
 
    $("#sortable").sortable(); 
 
    $("#sortable").disableSelection(); 
 
    }); 
 

 
    </script> 
 

 
</head> 
 
<body> 
 

 
\t <iframe src="http://free.timeanddate.com/clock/i5zr5d5i/n1991/szw110/szh110/hoc09f/hbw0/hfc09f/cf100/hnce1ead6/fas30/fdi66/mqc000/mql15/mqw4/mqd98/mhc000/mhl15/mhw4/mhd98/mmc000/mml10/mmw1/mmd98/hhc00f/hhs2/hmcf00/hms2/hsv0" frameborder="0" width="110" height="110"></iframe> 
 

 

 
\t <div id="container"> 
 
\t \t <h1>To-do List <i class="fa fa-plus"></i></h1> 
 
\t \t <form id="task-list"> 
 
\t \t <input type="text" placeholder="Add a To-Do" id="task"> 
 
\t </form> 
 
\t \t <ol id="sortable"> 
 
\t \t \t <li><span><i class="fa fa-trash"></i></span> EXAMPLE</li> 
 
\t \t \t 
 
\t \t </ol> 
 
\t </div> 
 

 
\t 
 

 
<script type="text/javascript" src="Utd.js"></script> 
 
</body> 
 
</html>

+0

申し訳ありません、これは私がここに初めて参加したことであり、タイトルがつぶれています。ごめんなさい。 –

+0

質問を編集して修正できますか?それはぎこちないタイトルだけではありません。 – roelofs

+0

どうしたのですか?本当にごめんなさい。このウェブサイトには本当にごめんなさい。 –

答えて

0

//check of spec 
 
$("ol").on("click", "li", function(){ 
 
$(this).toggleClass("completed"); 
 

 
}); 
 

 
//click on X to delete To-DO 
 

 
$("ol").on("click", "span", function(event){ 
 
\t $(this).parent().fadeOut(500,function(){ 
 
\t \t $(this).remove(); 
 
\t }); 
 
\t event.stopPropagation(); 
 
}); 
 

 
$("input[type='text'").keypress(function(event){ 
 
\t if(event.which === 13) { 
 
\t \t //grabbing the text typed 
 
\t \t var todoText = $(this).val(); 
 
\t \t $(this).val(""); 
 
\t \t //create a new LI and add to UL 
 
\t \t $("ol").append("<li><span><i class='fa fa-trash'></i></span> " + todoText +"</li>") 
 
\t } 
 
}); 
 

 
$(".fa-plus").click(function(){ 
 
\t $("input[type='text'").fadeToggle(); 
 
});
h1 { 
 
\t background: #2980b9; 
 
\t color: white; 
 
\t margin: 0; 
 
\t padding: 10px 15px; 
 
\t text-transform: uppercase; 
 
\t font-size: 24px; 
 
\t font-weight: normal; 
 

 
} 
 

 
iframe { 
 
\t float: left; 
 
} 
 

 
ol { 
 
\t /* THE BULLET POINTS 
 
\t list-style: none; 
 
\t */ 
 
\t margin: 0; 
 
\t padding: 0; 
 
\t font-size: 18px; 
 

 
} 
 

 
body { 
 

 
background-color: rgb(13, 168, 108); 
 

 
} 
 

 
li { 
 
\t background: #fff; 
 
\t height: 30px; 
 
\t line-height: 30px; 
 
\t color: #666; 
 
} 
 

 
li:nth-child(2n){ 
 
\t background: #d3d3d3; 
 
} 
 

 
span { 
 

 
\t height: 30px; 
 
\t width: 0px; 
 
\t margin-right: 20px; 
 
\t text-align: center; 
 
\t color:white; 
 
\t display: inline-block; 
 
\t transition: 0.2s linear; 
 
\t opacity:0; 
 
\t background: #e74c3c 
 
\t 
 

 
} 
 

 
li:hover span { 
 
\t width: 30px; 
 
\t opacity: 1.0; 
 
} 
 

 
input { 
 
\t font-size: 18px; 
 
\t width: 100%; 
 
\t padding: 13px 13px 13px 20px; 
 
\t box-sizing: border-box; 
 
\t border: 3px solid rgba(0,0,0,0); 
 
\t color: #2980b9; 
 
\t background-color: #e4e4e4; 
 

 
} 
 

 
input:focus { 
 
\t background: white; 
 
\t border: 3px solid green; 
 

 
\t /*OUTLINE OF INPUT BOX 
 
\t outlin: none; */ 
 
} 
 

 
.fa-plus { 
 
\t float: right; 
 
} 
 

 

 
#container { 
 
\t 
 
\t width: 360px; 
 
\t margin: 60px auto; 
 
\t background: #d3d3d3; 
 
\t box-shadow: 0 0 3px rgba(0,0,0,0.1); 
 

 

 
} 
 

 
.completed { 
 
\t color: red; 
 
\t text-decoration: line-through; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title>ToDo List</title> 
 
\t <link rel="stylesheet" type="text/css" href="utd.css"> 
 
\t <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> 
 
\t <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css"> 
 

 
\t <script type="text/javascript" src="jquery-3.2.1.min.js"></script> 
 
\t <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 
\t <script> 
 
    $(function() { 
 
    $("#sortable").sortable(); 
 
    $("#sortable").disableSelection(); 
 
    }); 
 

 
    </script> 
 

 
</head> 
 
<body> 
 

 
\t <iframe src="http://free.timeanddate.com/clock/i5zr5d5i/n1991/szw110/szh110/hoc09f/hbw0/hfc09f/cf100/hnce1ead6/fas30/fdi66/mqc000/mql15/mqw4/mqd98/mhc000/mhl15/mhw4/mhd98/mmc000/mml10/mmw1/mmd98/hhc00f/hhs2/hmcf00/hms2/hsv0" frameborder="0" width="110" height="110"></iframe> 
 

 

 
\t <div id="container"> 
 
\t \t <h1>To-do List <i class="fa fa-plus"></i></h1> 
 
\t \t <form id="task-list"> 
 
\t \t <input type="text" placeholder="Add a To-Do" id="task"> 
 
\t </form> 
 
\t \t <ol id="sortable"> 
 
\t \t \t <li><span><i class="fa fa-trash"></i></span> EXAMPLE</li> 
 
\t \t \t 
 
\t \t </ol> 
 
\t </div> 
 

 
\t 
 

 
<script type="text/javascript" src="Utd.js"></script> 
 
</body> 
 
</html>

//check of spec 
 
$("ol").on("click", "li", function(){ 
 
$(this).toggleClass("completed"); 
 

 
}); 
 

 
//click on X to delete To-DO 
 

 
$("ol").on("click", "span", function(event){ 
 
\t $(this).parent().fadeOut(500,function(){ 
 
\t // \t $(this).remove(); 
 
\t }); 
 
\t //event.stopPropagation(); 
 
}); 
 

 
$("input[type='text'").keypress(function(event){ 
 
\t if(event.which === 13) { 
 
\t \t //grabbing the text typed 
 
\t \t var todoText = $(this).val(); 
 
\t \t $(this).val(""); 
 
\t \t //create a new LI and add to UL 
 
\t \t $("ol").append("<li><span><i class='fa fa-trash'></i></span> " + todoText +"</li>") 
 
\t } 
 
}); 
 

 
$(".fa-plus").click(function(){ 
 
\t $("input[type='text'").fadeToggle(); 
 
});
h1 { 
 
\t background: #2980b9; 
 
\t color: white; 
 
\t margin: 0; 
 
\t padding: 10px 15px; 
 
\t text-transform: uppercase; 
 
\t font-size: 24px; 
 
\t font-weight: normal; 
 

 
} 
 

 
iframe { 
 
\t float: left; 
 
} 
 

 
ol { 
 
\t /* THE BULLET POINTS 
 
\t list-style: none; 
 
\t */ 
 
\t margin: 0; 
 
\t padding: 0; 
 
\t font-size: 18px; 
 

 
} 
 

 
body { 
 

 
background-color: rgb(13, 168, 108); 
 

 
} 
 

 
li { 
 
\t background: #fff; 
 
\t height: 30px; 
 
\t line-height: 30px; 
 
\t color: #666; 
 
} 
 

 
li:nth-child(2n){ 
 
\t background: #d3d3d3; 
 
} 
 

 
span { 
 

 
\t height: 30px; 
 
\t width: 0px; 
 
\t margin-right: 20px; 
 
\t text-align: center; 
 
\t color:white; 
 
\t display: inline-block; 
 
\t transition: 0.2s linear; 
 
\t opacity:0; 
 
\t background: #e74c3c 
 
\t 
 

 
} 
 

 
li:hover span { 
 
\t width: 30px; 
 
\t opacity: 1.0; 
 
} 
 

 
input { 
 
\t font-size: 18px; 
 
\t width: 100%; 
 
\t padding: 13px 13px 13px 20px; 
 
\t box-sizing: border-box; 
 
\t border: 3px solid rgba(0,0,0,0); 
 
\t color: #2980b9; 
 
\t background-color: #e4e4e4; 
 

 
} 
 

 
input:focus { 
 
\t background: white; 
 
\t border: 3px solid green; 
 

 
\t /*OUTLINE OF INPUT BOX 
 
\t outlin: none; */ 
 
} 
 

 
.fa-plus { 
 
\t float: right; 
 
} 
 

 

 
#container { 
 
\t 
 
\t width: 360px; 
 
\t margin: 60px auto; 
 
\t background: #d3d3d3; 
 
\t box-shadow: 0 0 3px rgba(0,0,0,0.1); 
 

 

 
} 
 

 
.completed { 
 
\t color: red; 
 
\t text-decoration: line-through; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title>ToDo List</title> 
 
\t <link rel="stylesheet" type="text/css" href="utd.css"> 
 
\t <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> 
 
\t <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css"> 
 

 
\t <script type="text/javascript" src="jquery-3.2.1.min.js"></script> 
 
\t <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 
\t <script> 
 
    $(function() { 
 
    $("#sortable").sortable(); 
 
    $("#sortable").disableSelection(); 
 
    }); 
 

 
    </script> 
 

 
</head> 
 
<body> 
 

 

 
\t <div id="container"> 
 
\t \t <h1>To-do List <i class="fa fa-plus"></i></h1> 
 
\t \t 
 
\t \t <input type="text" placeholder="Add a To-Do" id="task"> 
 

 
\t \t <ol id="sortable"> 
 
\t \t \t <li><span><i class="fa fa-trash"></i></span> EXAMPLE</li> 
 
\t \t \t 
 
\t \t </ol> 
 
\t </div> 
 

 
\t 
 

 
<script type="text/javascript" src="Utd.js"></script> 
 
</body> 
 
</html>

+0

これをローカルストレージにすべてのデータを保存できる場所にする方法はありますか? –

+0

SQL SBのローカル記憶手段ですか? –

+0

どうすればいいですか? –

0

ハッピーホリデー!

あなたのコーディングの願いが認められます。これはあなたへの贈り物です。あなたは良い人でなくてはならない優れた例を掲示し、人々があなたのためにコードを書くのを忘れていることを忘れないでください。

あなたの例に欠けているものがたくさんありました。

  1. リストに項目がリストは
  2. が更新されたときに
  3. がリスト
    • アップデートのローカルストレージをソートすることを許可リストに追加され
      • アップデートのローカルストレージを項目を追加します。
    • タスク項目をc
      • アップデートのローカルストレージをompleted項目が完了したとき
    • は、タスクが
  4. ロードローカルに保存されているタスク
を削除されたときにタスクアイテムが
  • アップデートのローカルストレージを削除することを許可

    私はそれが前夜このスクリプトがやりたいと思っていたことです。今はもっと明らかになっていますか?

    実施例:https://jsfiddle.net/Twisty/ae6oLr47/12/

    HTML

    <iframe src="https://free.timeanddate.com/clock/i5zr5d5i/n1991/szw110/szh110/hoc09f/hbw0/hfc09f/cf100/hnce1ead6/fas30/fdi66/mqc000/mql15/mqw4/mqd98/mhc000/mhl15/mhw4/mhd98/mmc000/mml10/mmw1/mmd98/hhc00f/hhs2/hmcf00/hms2/hsv0" frameborder="0" width="110" 
    height="110"></iframe> 
    <div id="container"> 
        <h1>To-do List <i class="fa fa-plus"></i></h1> 
        <form id="task-list"> 
        <input type="text" placeholder="Add a To-Do" id="task"> 
        <button type="submit"></button> 
        </form> 
        <ol id="sortable"> 
        <li id="task-EXAMPLE"><span><i class="fa fa-trash"></i></span> 
         <label>EXAMPLE</label> 
        </li> 
        </ol> 
    </div> 
    

    初めてこの負荷は、何のストレージは存在しませんので、我々は、HTMLからの例を読むことができます。ご覧のとおり、更新を行うと、これはもう適用されません。

    質問:なぜ<button>

    A:<form>には送信ボタンがあります。それはそれを必要としませんそれを持っているが、私はこの質問のために行きたくない方法で多くの助けになります。

    はJavaScript

    $(function() { 
        $("#sortable").on("click", function(event) { 
        console.log(event.target); 
        var $thatItem = $(event.target).parents("li"); 
        switch (event.target.nodeName) { 
         case "SPAN": 
         case "I": 
         $thatItem.fadeOut(500, function() { 
          $thatItem.remove(); 
          $("#sortable").sortable("refresh"); 
         }); 
         break; 
         case "LABEL": 
         $thatItem.toggleClass("completed"); 
         break; 
        } 
        setTimeout(function() { 
         updateLocalStorage($("#sortable")); 
        }, 500); 
        event.stopPropagation(); 
        }); 
    
        $("#task-list").submit(function(event) { 
        event.preventDefault(); 
        // Grabbing the text typed 
        var todoText = $("#task").val(); 
        addListItem($("#sortable"), todoText, false); 
        // Clear the text field 
        $("#task").val(""); 
        updateLocalStorage($("#sortable")); 
        }); 
    
        $(".fa-plus").click(function() { 
        $("#task-list").fadeToggle(); 
        }); 
        $("#sortable").sortable({ 
        update: function(e, ui) { 
         updateLocalStorage($(this)); 
        } 
        }).disableSelection(); 
    
        function addListItem($t, s, c) { 
        //create a new LI 
        var $li = $("<li>", { 
         id: "task-" + s.replace(" ", "_") 
        }); 
        if (c) { 
         $li.addClass("completed"); 
        } 
        var $wrap = $("<span>").appendTo($li); 
        $wrap.append($("<i>", { 
         class: "fa fa-trash" 
        })); 
        $li.append($("<label>").html(s)); 
        $li.appendTo($t); 
        $t.sortable("refresh"); 
        } 
    
        function updateLocalStorage($list) { 
        var tasks = {}; 
        $list.find("li").each(function(ind, elem) { 
         var task = $(elem).text().trim(); 
         var completed = $(elem).hasClass("completed"); 
         tasks[task] = completed; 
         if (typeof(Storage) !== "undefined") { 
         localStorage.setItem("tasks", JSON.stringify(tasks)); 
         } 
        }); 
        } 
    
        if (typeof(Storage) !== "undefined") { 
        // Code for localStorage/sessionStorage. 
        if (localStorage.getItem("tasks") !== "undefined") { 
         var localTasks = JSON.parse(localStorage.getItem("tasks")); 
         // Grab stored tasks 
         $("#sortable").find("li").remove(); 
         $.each(localTasks, function(task, status) { 
         addListItem($("#sortable"), task, status); 
         }); 
        } 
        } else { 
        // Sorry! No Web Storage support.. 
        } 
    }); 
    

    あなたはここに残って、あなたの元のコードの非常に少ないがあることが表示される場合があります。コードを改善する場所は数多くありました。私は簡単に少し話します。動的に作成しようとしている

    クリックしてイベント


    私たちは基本的に同じ親のクリックイベントをリッスンが、特定の要素がクリックされたときに異なる事をやりたいしているので、我々はを利用することができますclickイベントのevent.targetクリックされたのがspanまたはiの場合は、別の場合はlabelの1つです。

    setTimeoutは、スイッチから更新までの操作の遅延を作成する単なる方法です。それ以外の場合、更新はほぼスイッチの上で実行され、リストの変更は表示されません。このレコードは変更されません。

    あなたがを打つときは、基本的にフォームを提出している戻りまたはを入力します。イベント


    を提出してください。送信ボタンを追加する理由の一部。 keypressをキャッチしてこれを防止しようとするのではなく、submitイベントを私たちの利益に使うのはなぜでしょうか。このメソッドは、モバイルブラウザなどに役立ちます。

    ソート可能な更新イベント


    リストがソートされ、更新されたとき、私たちは、ローカルストレージにその順序を更新する必要があります。今はそれを行う時です。

    機能


    私は、これらはかなりはっきりしていると思います。あなたは何度も繰り返して、操作を書いて操作します。

    のGet項目


    ページは、すべての準備ができたときに、コードの最後のビットがロードされます。 localStorageを確認し、tasksが格納されているかどうかを確認します。その後、リストにそれらを入力します。

    Q:JSON.stringify()とはなんですか?

    A:はい、ローカルにものを格納することはできますが、文字列としてのみ使用できます。これにより、格納用オブジェクトの文字列バージョンが作成されます。

    その他のご質問がある場合は、ご意見ください。

+0

申し訳ありませんが、これは初めての投稿ですが、私の投稿を非常に良いものにする方法はまだ分かりませんが、試してみますより良い –

関連する問題