我是靠谱客的博主 舒心盼望,最近开发中收集的这篇文章主要介绍vue 怎么在字符串中指定位置插入字符_在VUE JS中的Cursor位置插入字符,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

I have been trying to insert emoji in textarea exactly where the cursor is at. I looked around how tos in the web could not find anything specific in VUE JS. Most of them are in plain JS.

I have this Code

set ="messenger"

title="Pick your emoji…"

emoji="point_up"

@click="addEmoji"

:emoji-size="16"

>

type="text"

v-model="greeting_text"

rows="8"

required

placeholder="Hi {first-name}! Welcome to our bot. Click on the ‘Get

Started’ button to begin

">

My Method

addEmoji(emoji){

this.greeting_text += emoji.native;

this.showPicker = !this.showPicker;

}

Obviously, this code will add the character (emoji, in my case) to the last of the string. I need a pure vuejs solution for this.

What would be the best practise for this kind of problem in Vue? as there are few solutions in the web that based either in vanilla JS or Jquery.

解决方案

Two steps:

1 get textarea element using a vue-way:

1.1 Add ref attrbute to textarea tag in your template code:

1.2 get this element after mounted hook of this component:

let textarea = this.$refs.ta

2 get cursor position of textarea element.

let cursorPosition = textarea.selectionStart

Here is reference: ref

最后

以上就是舒心盼望为你收集整理的vue 怎么在字符串中指定位置插入字符_在VUE JS中的Cursor位置插入字符的全部内容,希望文章能够帮你解决vue 怎么在字符串中指定位置插入字符_在VUE JS中的Cursor位置插入字符所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部