鱼C论坛

 找回密码
 立即注册
查看: 1376|回复: 0

为何点击添加按钮后,创建的li会立即消失

[复制链接]
发表于 2022-4-28 16:02:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>

  8.     <title>Document</title>
  9.     <style>
  10.         input{
  11.             display:inline-block;
  12.         }
  13.         .one{
  14.             width:500px;
  15.             height:400px;
  16.             background-color:yellow;
  17.         }
  18.         .two{
  19.             width:500px;
  20.             height:500px;
  21.             background-color:aqua;
  22.         }
  23.     </style>
  24. </head>
  25. <body>
  26.     <form>
  27.     <input id="usname" value="sd" type="text">
  28.     <input id="checked"  type="checkbox">
  29.     <button>添加</button>
  30. </form>
  31. <div class="one">
  32.     <ol></ol>
  33. </div>
  34. <div class="two">
  35.     <ol></ol>
  36. </div>
  37.     <script>
  38.         var date=[
  39.             {
  40.                 title:'未来很美好',
  41.                 done:false,
  42.             },

  43.             {
  44.                 title:'美好的一天',
  45.                 done:false,
  46.             },
  47.         ];
  48.         //本地存储里只能存储字符串的数据格式,把我们的数组对象转换为字符串格式 JSON.stringfy();
  49.         localStorage.setItem('todo',JSON.stringify(date));
  50.         //
  51.         let ipt=document.querySelector('#usname');
  52.         const checked=document.querySelector('#checked');
  53.         const  btn=document.querySelector('button');
  54.         const one=document.querySelector('.one');
  55.         const two=document.querySelector('.two');
  56.         const ol1=one.querySelector('ol');
  57.         const  ol2=two.querySelector('ol');
  58.         console.log(ol1);
  59.         console.log(ol2);
  60.         btn.addEventListener('click',function(){
  61.             let val=ipt.value;
  62.             localStorage.setItem('uname',val);
  63.             let news=localStorage.getItem('todo',date);
  64.         let li= document.createElement('li');
  65.         ol1.appendChild(li);
  66.         li.innerHTML=news;
  67.         });


  68. </script>
  69. </body>
  70. </html>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 18:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表