用js写小黑记事本

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Document</title>
  <style>
    h2 {
      text-align: center;
      margin-top: 100px;
    }

    body {
      text-align: center;
    }

    input {
      width: 300px;
      height: 30px;
      border-radius: 5px;
      font-size: 16px;
      padding-left: 10px;
      outline: none;
      border: 1px solid #000;
    }

    button {
      width: 32px;
      height: 32px;
      border-radius: 5px;
      box-shadow: none;
      border: none;
      font-size: 21px;
      cursor: pointer;
    }

    p {
      color: #aaa;
    }

    .items {
      list-style: none;
      padding: 0;
      width: 350px;
      margin: 20px auto;
    }

    .items .item {
      font-size: 16px;
      line-height: 32px;
      text-align: left;
      cursor: pointer;
      border-bottom: 1px solid #eee;
    }

    button.sub {
      width: 25px;
      height: 25px;
      font-size: 12px;
      float: right;
      margin-top: 5px;
    }

    button.sub:hover {
      color: red;
    }
  </style>
</head>

<body>
  <!-- 模板 -->
  <div id="app">
    <h2>小黑记事本</h2>
    <!-- 输入非空的内容,点击+按钮后,如果内容不为空,则将新的记录添加到列表中,同时清空当前输入的内容 -->
    <input type="text" placeholder="请输入非空内容,点击加号记录" />
    <button class="add"> +</button>
    <!-- 显示用户待办事项的列表,双击当前列表项或者点击X按钮后,删除当前记录 -->
    <ul class='items'>
      <!-- <li class="item">
        吃饭
        <button class="sub">X</button>
      </li> -->

    </ul>
    <p>双击栏目删除对应数据</p>
  </div>
  <script>
    // 页面进来就要给+绑定点击事件
    (function () {
      $('.add').addEventListener('click', clickHandler);
      // 使用事件委托,将删除操作绑定给ul
      $('ul').addEventListener('click', delHandler);

    })();
    let todos = [];
    function clickHandler() {
      // console.log(this);
      //  获取input中的value
      let input = $('input[type=text]');
      let val = input.value.trim();
      // console.log(val);
      // 为空结束代码执行
      if (!val) return;
      // 将数据添加到任务列表中
      todos.push(val)
      // 清空input
      input.value = '';
      render();
    }
    // 渲染到页面中
    // function render() {
    //   // console.log(todos);
    //   // 追加之前先清空
    //   $('.items').innerHTML = '';
    //   todos.forEach((v, k) => {
    //     let li = document.createElement('li');
    //     li.className = 'item';
    //     li.innerHTML = v;
    //     // 将索引设置到li标签的自定义属性上
    //     li.dataset.key = k;
    //     let btn = document.createElement('button');
    //     btn.className = 'sub';
    //     btn.innerHTML = 'X';
    //     li.appendChild(btn);
    //     // console.log(li);
    //     $('.items').appendChild(li)
    //   })
    // }

    function render() {
      // console.log(todos);
      // 追加之前先清空
      $('.items').innerHTML = '';
      todos.forEach((v, k) => {
        let li = createEle('li', {
          className: 'item',
          innerHTML: v,
        });
        li.dataset.key = k;
        let btn = createEle('button', {
          innerHTML: 'X',
          className: 'sub'
        });
        // console.log(btn);
        // 将btn追加到li中
        li.appendChild(btn)
        $('.items').appendChild(li)
      })
    }
    //创建节点的函数
    function createEle(nodeName, obj) {
      let newEle = document.createElement(nodeName);
      // 创建完成要设置属性
      Object.assign(newEle, obj);
      // console.log(newEle);
      return newEle


    }

    // 将所有li的点击事件都委托给ul处理,判断事件源
    function delHandler(eve) {
      // console.log(eve.target);
      // console.log(eve.target.nodeName);
      // 判断点击的是否为button按钮
      if (eve.target.nodeName == 'BUTTON') {
        // 获取其父级节点
        let li = eve.target.parentNode;
        // console.log(li);
        li.remove();
        // console.log(todos);
        // 获取元素对应的缩索引
        let index = li.dataset.key;
        // console.log(index);
        // 根据索引删除数组一个元素
        todos.splice(index, 1)
      }


    }



    // 获取的方法
    function $(ele) {
      // 如果获取的结果只有一个元素 则返回该元素
      // 如果获取的结果是有多个元素组成的伪数组,则返回该伪数组
      let res = document.querySelectorAll(ele);
      // console.log(res);
      return res.length == 1 ? res[0] : res;

    }
      // console.log($('li'));

    // console.log($('.items'));
  </script>

</body>

</html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
小黑记事本是一个使用Vue框架编的项目,实现了简易的记事本功能。下面是部分小黑记事本的Vue源代码: 模板部分: ```html <template> <div class="notebook"> <h1>小黑记事本</h1> <div class="input-container"> <textarea v-model="content" placeholder="开始编"></textarea> </div> <div class="button-container"> <button @click="saveNote">保存</button> <button @click="clearNote">清空</button> </div> <div class="notes-container"> <h2>已保存的笔记</h2> <ul> <li v-for="(note, index) in notes" :key="index">{{ note }}</li> </ul> </div> </div> </template> ``` 脚本部分: ```javascript <script> export default { data() { return { content: '', notes: [], }; }, methods: { saveNote() { if (this.content.trim() !== '') { this.notes.push(this.content.trim()); this.content = ''; } }, clearNote() { this.content = ''; }, }, }; </script> ``` 样式部分: ```css <style scoped> .notebook { text-align: center; margin: 20px auto; } .input-container { margin-bottom: 20px; } textarea { width: 300px; height: 150px; } .button-container button { margin: 0 10px; } .notes-container { margin-top: 30px; } ul { list-style-type: none; padding-left: 0; } li { margin-top: 10px; } </style> ``` 以上是小黑记事本的一个简化版本,实现了输入内容的保存和清空,并将已保存的笔记展示在界面上。用户可以在文本框中输入内容,点击保存按钮即可将内容添加到已保存的笔记列表中。点击清空按钮可以清空文本框中的内容。小黑记事本提供了简单的界面交互,方便用户保存和管理笔记。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值