VUE留言板

效果预览图

完整代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>作业</title>
    <style>
        body{
            background: #ecdada;
        }
        *{
            margin: 0;
            padding: 0;
        }
        #app{
            width: 1000px;
            height: 100%;
            margin: 10px auto;
        }
        h2{
            text-align: center;
            margin-top: 10px;
        }
        .wp{
            width: 960px;
            height: 250px;
            background: pink;
            overflow: hidden;
            border-radius: 10px;
            padding: 20px;
        }
        textarea{
            display: block;
            margin: 5px auto;
            border-radius: 10px;
            padding: 10px;
            resize: none;
        }
        .wp button{
            display: block;
            width: 60px;
            height: 40px;
            background: #99ead6;
            margin-left: auto;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        input{
            width: 200px;
            height: 30px;
            margin-left: 5px;
            padding-left: 10px;
            border-radius: 5px;
        }
        .pl{
            width: 1000px;
            height: 180px;
            background: #fff;
            overflow: hidden;
            border-radius: 10px;
            margin-top: 20px;
        }
        span{
            width: 980px;
            height: 40px;
            display: block;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            margin-left: 10px;
        }
        p{
            margin: 0 0 10px 10px;
        }
        h3{
            margin: 10px;
        }
        button{
            display: inline-block;
            width: 60px;
            height: 40px;
            background: #99ead6;
            border: none;
            border-radius: 5px;
            margin-top: 10px;
            margin-left: 20px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div id="app">
    <h2>留言板</h2>
    <div class="wp">
         <input type="text" placeholder="给自己定义一个身份吧(默认匿名)" v-model="val">
        <textarea class="area" rows="10" cols="130" placeholder="有什么新鲜事想告诉大家" v-model="val2"></textarea>
        <button v-if="fl==1" @click="add">发布</button>
        <button  v-else-if="fl==2" @click="add2(jk)">提交修改</button>
    </div>
    <div class="pl" v-for="(item,index) in arr">
     <h3>{{arr[index].name}}</h3>
     <p>{{arr[index].times}}</p>
     <span>{{arr[index].cont}}</span>
     <button @click="sc(index)">删除</button>
     <button @click="xg(index)">修改</button>
    
    </div>
    
    </div>
</body>
</html>
<script type="module">
    import { createApp } from './js/vue.esm-browser.js';
    createApp({
         data() {
            return {
                arr:[
                    {name:'小姚学前端',times:'时间',cont:'学习111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111,好好学习,天天向上'},
                ],
                val:'',
                val2:'',
                fl:1,
                jk:-1,
            }
         },
         methods:{
            add(){
                let nn = new Date().toLocaleString()
                if (this.val2=='') {
                    alert('内容不能为空')
                }else if (this.val2!='') {
                    let obj={
                    name:this.val==''? '来自匿名':this.val,
                    times:nn,
                    cont:this.val2
                }
                this.arr.unshift(obj)
                this.val2=''
                this.val=''
                this.fl=1
                }
                
            },
            add2(jk){
                let nn = new Date().toLocaleString()
                if (this.val2=='') {
                    alert('内容不能为空')
                }else if (this.val2!='') {
                    let obj={
                    name:this.val==''? '来自匿名':this.val,
                    times:nn+'修改',
                    cont:this.val2
                }
                this.arr[jk]=obj
                this.val2=''
                this.val=''
                this.fl=1
                }
            },
            sc(index){
                let userResponse = window.confirm("确定要删除留言吗");
            if (userResponse) {
                this.arr.splice(index,1)
            } else {
              
            }
            },
            xg(index){
                this.val2=this.arr[index].cont;
                this.val=this.arr[index].name
                this.fl=2
                this.jk=index
            }
         }
    }).mount('#app')
</script>
相关推荐
amy_jork1 小时前
npm删除包
开发语言·javascript·ecmascript
帧栈3 小时前
开发避坑指南(27):Vue3中高效安全修改列表元素属性的方法
前端·vue.js
max5006003 小时前
基于桥梁三维模型的无人机检测路径规划系统设计与实现
前端·javascript·python·算法·无人机·easyui
我命由我123454 小时前
软件开发 - 避免过多的 if-else 语句(使用策略模式、使用映射表、使用枚举、使用函数式编程)
java·开发语言·javascript·设计模式·java-ee·策略模式·js
萌萌哒草头将军4 小时前
Node.js v24.6.0 新功能速览 🚀🚀🚀
前端·javascript·node.js
AALoveTouch5 小时前
大麦APP抢票揭秘
javascript
rannn_1115 小时前
【Javaweb学习|黑马笔记|Day1】初识,入门网页,HTML-CSS|常见的标签和样式|标题排版和样式、正文排版和样式
css·后端·学习·html·javaweb
持久的棒棒君5 小时前
启动electron桌面项目控制台输出中文时乱码解决
前端·javascript·electron
小离a_a6 小时前
使用原生css实现word目录样式,标题后面的...动态长度并始终在标题后方(生成点线)
前端·css
郭优秀的笔记7 小时前
抽奖程序web程序
前端·css·css3