vue记事本小案例

这是一个使用Vue.js创建的简单记事本应用。HTML结构包含输入框和列表,用于添加和删除条目。Vue实例绑定了数据和方法,如`add`用于添加记事,`remove`用于删除,`clear`用于清空所有记事。应用具有响应式布局和基本的样式设计。

vue和HTML结构

<!DOCTYPE html>
<html lang="zh-cn">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="./css/index.css" />
  <title>记事本</title>
</head>

<body>
  <div id="app">
    <h2>记事本</h2>
    <div class="main">
      <input type="text" v-model="inputValue" @keyup.enter="add" autofocus="autofocus" autocomplete="off" placeholder="请输入内容!" />
      <ul>
        <li>
          <!-- 使用v-for更具数据生成列表结构 -->
          <div class="content" v-for="(item,index) in list">
            <span >{{index+1}}.{{item}}</span>   <!--用{{index+1}}显示数据的索引以及数据的每一项-->
            <button @click="remove(index)">❌</button> 
          </div>
          
        </li>
      </ul>
      <p class="empty">空空如也...</p>
    </div>
    <div class="bottom">
      <span>合计:{{list.length}}</span>
      <span @click="clear()">清空</span>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  <script>
    new Vue({
      el: '#app',
      data: {
        list: ['吃饭','写代码','睡觉'],
        

      },
      methods: {
        add () {//添加数据的方法,使用v-on和keyup.enter事件绑定
          this.list.push(this.inputValue)  
        },
        remove(index){//删除数据方法
          this.list.splice(index,1);
        },
        clear(){
          this.list=[];//清空list
        }
      },
    })
  </script>
</body>

</html>

css样式

#app {
  text-align: center;
  background: #fbebd4;
  width: 480px;
  max-height: 400px;
  box-sizing: border-box;
  padding-top: 50px;
  border-radius: 5px;
  position: relative;
}

#app h2 {
  margin: 0;
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  color: #6a4d52;
  font-size: 30px;
}

#app .main {
  max-height: 300px;
  width: 454px;
  margin: 5px auto 0;
  transform: translateX(1px);
}

#app ul {
  padding: 0;
  max-height: 200px;
  overflow-y: scroll;
  margin-bottom: 0;
}

#app ul::-webkit-scrollbar {
  width: 0 !important;
}

#app li {
  list-style: none;
  border-bottom: 1px solid #6a4d52;
  text-align: left;
  box-sizing: border-box;
  cursor: pointer;
  font-size: 25px;
  color: #8895a8;
  display: flex;
  align-items: center;
  justify-content: space-between;
  line-height: 40px;
  display: block;
}

#app li:hover {
  background-color: #fcfaed;
}

#app li span {

  color: #6a4d52;
}

#app button {
  margin-left: 300px;
  cursor: pointer;
  border: none;
  background-color: transparent;
}

#app input {
  height: 30px;
  font-size: 20px;
  appearance: none;
  outline: none;
  border: none;
  padding-left: 5px;
  box-sizing: border-box;
  border-radius: 5px;
  color: #6a4d52;
}

#app .bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 50px;
  padding: 0 14px;
}

#app .bottom span {
  color: #929093;
}

#app .bottom span:last-child {
  cursor: pointer;
  color: #935256;
}

#app p {
  color: #929093;
  font-size: 20px;
  margin: 0;
  padding: 0;
}

 

### Vue.js 记事本 示例代码 以下是一个简单的 Vue.js 实现的记事本案例,用户可以添加、删除和查看笔记。此示例结合了 Vue 的核心概念,如数据绑定、事件处理和组件化。 #### HTML 部分 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Vue.js 记事本</title> <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> </head> <body> <div id="app"> <h3>我的记事本</h3> <input v-model="newNote" placeholder="输入新笔记" /> <button @click="addNote">添加笔记</button> <ul> <li v-for="(note, index) in notes" :key="index"> {{ note }} <button @click="deleteNote(index)">删除</button> </li> </ul> </div> <script> new Vue({ el: '#app', data: { newNote: '', notes: [] }, methods: { addNote() { if (this.newNote.trim() !== '') { this.notes.push(this.newNote); this.newNote = ''; // 清空输入框 } }, deleteNote(index) { this.notes.splice(index, 1); // 删除指定索引的笔记 } } }); </script> </body> </html> ``` #### 功能说明 1. **添加笔记**:通过 `v-model` 将输入框与 `newNote` 数据绑定,点击“添加笔记”按钮时调用 `addNote` 方法将内容添加到 `notes` 数组中[^1]。 2. **删除笔记**:使用 `v-for` 指令遍历 `notes` 数组并显示每条笔记,同时为每条笔记提供一个“删除”按钮,点击时调用 `deleteNote` 方法删除对应笔记[^4]。 --- ### 相关技术点 1. **双向数据绑定**:通过 `v-model` 实现输入框与 Vue 实例中的 `newNote` 数据同步[^1]。 2. **事件处理**:通过 `@click` 绑定点击事件,分别实现添加和删除功能。 3. **列表渲染**:使用 `v-for` 指令动态生成笔记列表,并为每条笔记提供唯一的 `key` 属性以优化性能[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值