VUE实现记事本功能

VUE实现记事本功能

直接上代码:

<!DOCTYPE html>
<html lang="en">
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
    <style>
        #app {
            margin: 0px auto;
            width: 500px;
            border: 1px solid gainsboro;
            height: auto;
        }
        .title {
            line-height: 50px;
            text-align: center;
            height: 50px;
            font-weight: 20;
            font-size: 36px;
            background: #42b983;
            border-bottom: 1px solid black;
        }

        /*.inp {*/
        /*    outline-style: none;*/
        /*    border: 0px solid #ccc;*/
        /*    width: 330px;*/
        /*    font-size: 15px;*/
        /*    padding: 3px 3px;*/
        /*    font-family: "Microsoft soft";*/
        /*}*/

        input:focus {
            border-color: #66afe9;
            outline: 0;
            -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 8px rgba(102, 175, 233, .6);
            box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 8px rgba(102, 175, 233, .6)
        }
        .file-container{
            overflow: hidden;
            margin-top: 10px;
        }
        .openfile-btn{
            float: left;
            margin-left: 10px;
        }
        #file_path{
            margin-left: 10px;
            width: 300px;
        }
        #file_con{
            display: block;
            border:0;
            border-radius:5px;
            background-color:rgba(241,241,241,.98);
            width: 480px;
            height: 250px;
            margin-top: 10px;
            margin-left: 10px;
            resize: none;
        }
        ul,
        li {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .li-div {
            text-align: center;
            /*border: 3px solid blanchedalmond;*/
        }
        .bot{
            height: 30px;
        }
        .del-btn{
            float: right;
            margin-right: 10px;
        }
    </style>
</head>

<body>
<div id="app">
    <div class="title">
        记事本
    </div>
    <div>
<!--        <span>-->
<!--            <p style="color: black; margin-left: 10px">请输入事件标题:-->
<!--                <input type="text" placeholder="按回车保存" class="inp" v-model="tmp" @keyup.enter="add">-->
<!--            </p>-->
<!--        </span>-->
        <div class="file-container">
            <input class="openfile-btn" type="button" value="从本地导入" id="fileImport" v-on:click="clickLoad">
            <input type="file" id="files" ref="refFile" style="display: none" v-on:change="fileLoad">
            <input type="text" v-model="path" id="file_path" disabled="disabled">
            <input type="button" value="确定导入" style="float:right; margin-right: 10px " v-on:click="addfile"></button>
            <textarea type="text" id="file_con" autoHeight="true" v-model="input_file"></textarea>
        </div>

    </div>
    <hr>
    <div class="content">
        <ul>
            <li v-for="(item, index) in message">
                <div class="li-div">
                    <span>{{++index}}</span>
                    <label>{{item}}</label>
                    <button @click="remove(index)" class="del-btn">删除</button>
                </div>
            </li>
        </ul>
    </div>
    <hr>
    <div v-show="message.length>0" class="bot">
        <div style="float: left; margin-left: 10px">
            当前记事本记录数:{{message.length}}
        </div>
        <div class="del-btn">
            <button @click="clear">清空</button>
        </div>
    </div>
</div>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            //tmp: "",
            message: [],
            path:'',
            input_file:''

        },
        methods: {
            clickLoad: function (){
                this.$refs.refFile.dispatchEvent(new MouseEvent('click'))
            },
            fileLoad() {
                var oFReader = new FileReader();
                const selectedFile = this.$refs.refFile.files[0];
                var name = selectedFile.name; //选中文件的文件名
                var size = selectedFile.size; //选中文件的大小
                var reader = new FileReader();
                // oFReader.readAsDataURL(selectedFile);
                // oFReader.onload = function () {
                //     // src = oFRevent.target.result;
                //     console.info("路径为:" + this.result)
                // };
                reader.readAsText(selectedFile);
                this.path = name;
                console.log("文件名:" + name + "大小:" + size);

                reader.onload = function() {

                    let file_s = this.result;
                    document.getElementById('file_con').value=file_s
                }
            },
            // add: function () {
            //     if (this.tmp == null || this.tmp == "") {
            //         alert("输入不能为空");
            //     } else {
            //         this.message.push(this.tmp);
            //         this.tmp=""
            //     }
            // },
            addfile:function (){
                var file = document.getElementById('file_con').value;
                this.input_file=file;
                console.log("this.input_file:"+this.input_file)
                if (file == null || file == "") {
                    alert("输入不能为空");
                } else {
                    this.message.push(this.input_file);
                    this.input_file=''
                    this.path=''

                }
            },
            remove: function (index) {
                var flag = confirm("是否要删除删除" + index);
                if (flag == true) {
                    this.message.splice(index-1, 1);
                }
            },
            clear: function () {
                this.message = [];
            },
        },
    })
</script>
</body>
</html>

后续还要加…

上一篇:响应式布局


下一篇:【无标题】