uniapp设置editor自动获取焦点的办法

在uniapp中editor没有自带的方法可以获取焦点,:foucus只适用于input和textarea,如果需要editor自动获取到焦点,可以通过如下方法实现

上代码:

template
<template>
    <view class="_box">
         <view class="_hide">
               <input focus="true" type="text" value="" />  
         </view>          
         <editor id="editor" @ready="editorReady"></editor>
     </view>
</template>
css
    ._box{
        position:relative;

    }

    ._hide{
        position: absolute;
        z-index: -1;
    }
JS
<script>
    export default {
        data() {
            return {
                content:'',
            }
        },
        onLoad(param) {
        },
        methods: {
            editorReady() {
                uni.createSelectorQuery().select('#editor').context((res) => {
                    this.content = res.context;
                    setTimeout(()=>{
                        this.content.format("align", "left")
                    },100)
                }).exec();
            },
        }
    }
</script>

 

原理:

通过在editor上方前一位添加一个默认获取焦点的input,然后通过
this.content.format("align", "left")切换焦点,实现editor自动获取焦点的效果
 

 

 
posted @ 2022-06-29 20:37  赤炎斩风  阅读(3512)  评论(0)    收藏  举报