js localStorage jsonオブジェクトをローカルに格納し、データを追加


localStorageのもっと関連する知識を記録してください.http://www.w3school.com.cn/html5/html_5_webstorage.aspここでは主にlocalStorageがオブジェクトを格納する際に追加データが元のデータを上書きしないことを説明し、以下に小さなdemoを書きます



	
	
	
	<style>
		table{
			width: 300px;
			border: 1px #000 solid;
		}
		td,th{
			border: 1px #000 solid;
		}
	</style>


	  :<input type="text" class="name" placeholder="  "/><br/>
	  :<input type="text" class="age" placeholder="  "/><br/>
	  :<input type="text" class="sex" placeholder="  "/><br/><br/>
	<button class="top" onclick="btn()">  </button>
	<hr/>
	<table>
		<caption>    </caption>
		<thead>
			<tr>
				<th>  </th>
				<th>  </th>
				<th>  </th>
			</tr>
		</thead>
		<tbody class="tt">
		</tbody>
	</table>

<disable_script type="text/javascript">
	// localStorage.removeItem('data');  //      
	var jsonData = JSON.parse(localStorage.getItem('data'));
	console.log(jsonData);
	if(!jsonData){
		jsonData = [];
	}
	else{
		//    
		show();
	}
	function btn(){
		if(!$(".name").val() || !$(".age").val() || !$(".sex").val()){
			alert("       ");
		}
		else{
			jsonData.push({
				name:$(".name").val(),
				age:$(".age").val(),
				sex:$(".sex").val()
			});
			console.log(jsonData);
			$(".name").val("");
			$(".age").val("");
			$(".sex").val("");
			localStorage.setItem('data',JSON.stringify(jsonData));
			$(".tt").empty();
			show();
		}
	}
	// DOM      
	function show(){
		for(key in jsonData){
			$(".tt").append(`
				<tr>
					<td>${jsonData[key].name}</td>
					<td>${jsonData[key].age}</td>
					<td>${jsonData[key].sex}</td>
				</tr>
			`);
		}
	}	
</disable_script>
</code></pre>  <br/>効果図は以下の<a href="https://img.md5.com/image/info8/924b1cdd22c74005857736b2b56145eb.jpg" rel="noreferrer noopener nofollow"><img alt="js localStorage本地存储json对象并追加数据_第1张图片" class="check_url_is_full" height="310" src="https://s1.md5.ltd/image/bcd13a2232f58a32d4c4e8ecd998687a.jpg" width="650" style="border:1px solid black;"/></a><div class="container-fluid p-0">

                                    <script async
                                            src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3219747971601011"
                                            crossorigin="anonymous"></script>
                                    <!-- jpdebug-blog-source-div -->
                                    <ins class="adsbygoogle"
                                         style="display:block"
                                         data-ad-client="ca-pub-3219747971601011"
                                         data-ad-slot="3872614099"
                                         data-ad-format="auto"
                                         data-full-width-responsive="true"></ins>
                                    <script>
                                        (adsbygoogle = window.adsbygoogle || []).push({});
                                    </script>
                                </div>

                                


                            </div>

                            <div class="row mb-4  pt-4 pb-4"><div class="col-6 text-primary ">
                                    <a class=" btn btn-outline-dark  small  float-left  "
                                       href="/p/1746305">
                                        <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em"
                                             viewBox="0 0 24 24"
                                             fill="none" stroke="currentColor" stroke-width="2"
                                             stroke-linecap="round"
                                             stroke-linejoin="round" class="feather feather-arrow-left">
                                            <line x1="19" y1="12" x2="5" y2="12"></line>
                                            <polyline points="12 19 5 12 12 5"></polyline>
                                        </svg>

                                        最新のSelenium IDEを使ってヘッドレス実行したい!!っていう人向け情報(2018/04/23時点)</a>
                                </div><div class="col-6  text-primary ">
                                    <a class=" btn btn-outline-dark small float-right "
                                       href="/p/1746309">mallocとnewの違いは何ですか?
                                        <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em"
                                             viewBox="0 0 24 24"
                                             fill="none" stroke="currentColor" stroke-width="2"
                                             stroke-linecap="round"
                                             stroke-linejoin="round" class="feather feather-arrow-right">
                                            <line x1="5" y1="12" x2="19" y2="12"></line>
                                            <polyline points="12 5 19 12 12 19"></polyline>
                                        </svg>


                                    </a>
                                </div></div>


                        </article>


                    </div>

                    <!-- end row -->
                </div> <!-- end card-body -->
            </div> <!-- end card -->
        </div>
        <!-- end chat area-->

        <!-- start user detail -->
        <div class="col-12 col-xxl-3 col-xl-3 ">

            <div class="card sticky-top" style="top: 20px;">
                <div class="row">


                    <aside class="col-md-12 col-xxl-12 d-none d-md-block   pt-0">
                        <div class="sticky">

                            <div class="container-fluid p-0 m-0 bg-light-off ">
                                <script async
                                        src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3219747971601011"
                                        crossorigin="anonymous"></script>
                                <!-- jpdebug-right-top-1 -->
                                <ins class="adsbygoogle"
                                     style="display:inline-block;width:300px;height:600px"
                                     data-ad-client="ca-pub-3219747971601011"
                                     data-ad-slot="1251860546"></ins>
                                <script>
                                    (adsbygoogle = window.adsbygoogle || []).push({});
                                </script>
                            </div>


                        </div>

                    </aside>

                </div>
                <div class="card-body">


                    <div class="row ">


                        <aside class="col-md-12 col-xxl-12 d-none d-md-block  pt-3">
                            <div class="sticky"><ul id="toc" class="nav flex-column toc overflow-scroll">

                                    <h4 class="nav-link h4 relate_nav_title text-dark" disabled>関連内容</h4><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3094797">jQuery学習の旅Item 1セレクタ【一】</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3094520">いろいろな画像の特効がありますが、いかがですか.本当に欲しいんじゃないの?【純CSS実現】</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3093736">Webフロントエンド開発者はCSS属性を必ず知っている</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3092409">htmlが導入したjs cssファイルに乱数を追加し、毎回最新のファイルを取得することを保証し、ブラウザキャッシュの問題を解決することができる.</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3090889">css文字スクロール</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3089692">Swashbuckle Swagger UI MVC web api用</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3086142">cssは文字が垂直に中央に位置し、自動的に改行することを実現する</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3085440">jQueryのhoverイベントの小さなバグについて</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3508950">先週学んだこと: JavaScript 条件</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3508947">先週学んだこと: async と await の概要</a>
                                    </li></ul><!--                                <div class="container-fluid p-0">-->
<!--                                    <script async-->
<!--                                            src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3219747971601011"-->
<!--                                            crossorigin="anonymous"></script>-->
<!--                                    <!– jpdebug-post-right –>-->
<!--                                    <ins class="adsbygoogle"-->
<!--                                         style="display:block"-->
<!--                                         data-ad-client="ca-pub-3219747971601011"-->
<!--                                         data-ad-slot="8535334719"-->
<!--                                         data-ad-format="auto"-->
<!--                                         data-full-width-responsive="true"></ins>-->
<!--                                    <script>-->
<!--                                        (adsbygoogle = window.adsbygoogle || []).push({});-->
<!--                                    </script>-->
<!--                                </div>-->


                            </div>

                        </aside>
                    </div>
                </div> <!-- end card-body -->
            </div> <!-- end card-->
        </div> <!-- end col -->
        <!-- end user detail -->
    </div>


</div><!-- footer -->
<footer class="bg-light p-3 mt-5">
    <div class="container">
        <div class="row gutter-3">

            <div class="col-12 col-md-12 text-white text-center text-lowercase">
                <p class="mb-0 text-black-50 small text-lowercase">©2022 jpdebug.com. All Rights Reserved. | <a
                        href="/privacy" target="_blank" class="text-dark" rel="nofollow">Privacy Policy</a> | <a
                        href="/contact_us"
                        target="_blank"
                        class="text-dark" rel="nofollow">Contact
                    US</a> | <a href="/sitemap.xml" target="_blank" class="text-dark" rel="nofollow">Sitemap</a></p>

            </div>

        </div>
    </div>
</footer>
<!-- / footer -->

<!-- Bootstrap 4 Version -->
<div class="nk-cookie-banner alert alert-secondary text-center mb-0 small" role="alert"
     style="background: rgba(255, 255, 255, .6);border-color:#f2f2f2;-webkit-backdrop-filter: blur(10px);backdrop-filter: blur(20px);">
    🍪このウェブサイトは、あなたが我々のウェブサイトで最高の経験を得ることを確実とするために、クッキーを使います。
    <a href="/privacy" target="blank" rel="nofollow">プライバシー条項の表示</a>
    <button type="button" class="btn btn-primary btn-sm ml-3 shadow-none" onclick="window.nk_hideCookieBanner()">
        同意します
    </button>
</div><!-- JS Global Compulsory hs-builder:build-delete -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>


<script src="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.2.0/highlight.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js"></script>

<script src="/static/all_theme_package/quick-website.min.js"></script>

<script src="/static/all_theme_package/cookie-banner.js"></script><script>
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?d95bbf01f0dd90da512ecedf1d9661bd";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>

<script async src="//static.getclicky.com/101379076.js"></script>

<!--<script>-->
<!--    $(document).ready(function () {-->

<!--        $("pre").addClass("language-none");-->

<!--    });-->
<!--</script>-->
<script src="/static/all_theme_package/auto-enable-prism.js"></script>
<script src="/static/all_theme_package/prism.min.js"></script></body>

</html><script src="/cdn-cgi/scripts/7d0fa10a/cloudflare-static/rocket-loader.min.js" data-cf-settings="cd490767b8b4756cae25a67d-|49" defer></script>