• 模拟textarea----》contentable


    <template>
        <div class="edit-div"
             v-html="innerText"
             :placeholder="placeholder"
             :contenteditable="canEdit"
             @keydown.13="keyDown($event)"
             @focus="isLocked = true"
             @blur="isLocked = false"
             @input="changeText">
        </div>
    </template>
    <script>
        export default{
            name: 'v-edit-div',
            props: {
                value: {
                    type: String,
                    default: ''
                },
                placeholder: {
                    type: String,
                    default: ''
                },
                canEdit: {
                    type: Boolean,
                    default: true
                },
                //禁止换行
                nowrap: {
                    type: Boolean,
                    default: false
                }
            },
            data(){
                return {
                    innerText: this.value,
                    isLocked: false
                }
            },
            watch: {
                'value'(){
                    if (!this.isLocked && !this.innerText) {
                        this.innerText = this.value;
                    }
                }
            },
            methods: {
                changeText(){
                    // 解决:末尾换行,看不见的<br>,删不掉,造成清空无法出现placeholder文字
                    if(this.$el.innerHTML=="<br>"){
                        this.$el.innerHTML="";
                    }
                    this.$emit('input', this.$el.innerHTML);
                },
                keyDown(e){
                    console.log("回车键按下--nowrap--value:",this.nowrap)
                    if(this.nowrap){
                        e.preventDefault();
                    }
                }
            }
        }
    </script>
    <style>
        .edit-div {
             100%;
            height: 100%;
            overflow: auto;
            word-break: break-all;
            outline: none;
            user-select: text;
            white-space: pre-wrap;
            text-align: left;
        }
        .edit-div[contenteditable=true]{
            user-modify: read-write-plaintext-only;  
            -webkit-user-modify: read-write-plaintext-only;  
        }
        .edit-div[contenteditable=true]:empty:before {
            content: attr(placeholder);
            display: block;
            color: #ccc;
        }
    </style>
  • 相关阅读:
    使用junit进行单元测试
    初学软件工程.
    初学软件工程
    寻医问药软件
    使用JUnit工具进行单元测试
    软件工程问题
    JUnit进行单元测试
    软件工程学习问题
    单元测试
    软件工程
  • 原文地址:https://www.cnblogs.com/cs122/p/13254912.html
Copyright © 2020-2023  润新知