textarea在光标處插入一段文本
效果
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2YfNWawNCM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TP310MNR1T5VleOBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL2gDN0UjM1kDMyADNwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
實作
HTML:
<!-- 點選字型圖示在textarea中加入文本 -->
<span @click="addPictureClic">
<i class="fa fa-image" title="添加圖檔"></i>
</span>
<!-- ref="editorbox" 在方法中篩選出textarea -->
<textarea class='editor-text'
ref="editorbox"
v-model="content"/>
methods:
methods:{
addPictureClic(){
this.addStringTOTextarea('# 我是插入的圖檔');
},
addStringTOTextarea(str){
var tc = this.$refs.editorbox;
var tclen = tc.value.length;
tc.focus();
if(typeof document.selection != "undefined")
{
document.selection.createRange().text = str;
}
else
{
this.content = this.content.substr(0,tc.selectionStart)+str+this.content.substring(tc.selectionStart,tclen);
}
}
}