0


vue实现带样式的textarea输入框,contenteditable属性应用

提示:vue中使用contenteditable

文章目录


前言

开发需求,研究了一下,做个记录分享。
1、输入框文字提示带样式
在这里插入图片描述
2、输入内容文字字体颜色为黑色
在这里插入图片描述

3、查询操作完成后,未查询到的数据标红回显到输入框在这里插入图片描述

一、contenteditable

什么是contenteditable:

  1. contenteditable 属性指定元素内容是否可编辑。
  2. 个人理解就是元素标签加上这个属性就可以像input一样,可以输入内容了

二、contenteditable的使用

1、TextareaComponent.vue

  1. <template><div class="tctextarea_box"><div id="tctextarea" class="tctextarea" contenteditable="plaintext-only" @focus="focusTxt" @input="inputTxt" @blur="blurTxt" v-html="txtHtml"></div></div></template><script>
  2. export default{
  3. name:"TextareaComponent",
  4. computed:{},
  5. props:{},data(){return{
  6. tipData:['输入框第一条提示','输入框第二条提示','输入框第三条提示','输入框第四条提示','输入框第五条提示'],
  7. txtData:[],
  8. txtHtml:'',
  9. txtStr:'',}},created(){
  10. this.createTipHtml();},
  11. methods:{//textarea输入inputTxt(e){},//获取焦点focusTxt(e){//获取焦点时,输入框文字为黑色if(e.target.innerHTML){if(e.target.innerHTML.indexOf('#FF4019')>-1){
  12. let arr = e.target.innerHTML.split('#FF4019');
  13. let str ='';for(let i=0;i<arr.length;i++){
  14. str+=arr[i];if(i!==arr.length-1){
  15. str+='#323D4D';}}
  16. e.target.innerHTML = str;
  17. this.focusTextarea();}}//输入框获得焦点时内容为初始提示,清空提示if(this.txtHtml.indexOf(this.tipData[0])>-1){
  18. this.txtHtml ='';
  19. this.focusTextarea();}},//主动使textarea获取焦点focusTextarea(){
  20. let node = document.getElementById('tctextarea');if(window.getSelection){
  21. let range = window.getSelection();
  22. range.selectAllChildren(node);// range.collapseToStart();
  23. range.collapseToEnd();}else{
  24. let range = document.selection.createRange();
  25. range.moveToElementText(node);
  26. range.collapse(false);
  27. range.select();}},//失去焦点blurTxt(e){//如果输入内容为'' 出现提示
  28. let str = e.target.innerText?e.target.innerText.trim():'';if(str.length<1){
  29. this.createTipHtml();}else{
  30. this.txtStr = e.target.innerText;
  31. this.createInputHtml();}},//创建提示文字createTipHtml(){
  32. let temp ='';for(let i=0;i<this.tipData.length;i++){
  33. temp+=`<div class="tctip_item" style="font-size: 14px; color: ${i==0?'#F77700':'#A2AEBF'} ;line-height: 24px; display: flex; box-sizing: border-box; padding-bottom:5px;margin-:0;"><div class="tctip_l" style="width: 20px;">${i+1}.</div><div class="tctip_r" style="flex-grow:1;">${this.tipData[i]}</div></div>`
  34. }
  35. this.txtHtml = temp;},//创建输入内容createInputHtml(str,colorType,splitStr){
  36. let temp ='';
  37. let splitTxt = splitStr||'\n';
  38. let data= this.deleteSpace(str||this.txtStr).split(splitTxt);
  39. let newData =[];for(let i=0;i<data.length;i++){if(data[i]!=''){//输入内容文字默认为黑色 当输入内容查询不到时 显示为红色
  40. temp+=`<div class="tctxt_item" style="font-size:14px;color:${colorType?'#FF4019':'#323D4D'};line-height:24px;">${data[i]}</div>`;
  41. newData.push(data[i])}}
  42. this.txtHtml = temp;//触发父组件事件
  43. this.$emit('changeTextareaValue',newData);},//删除字符串中的空格deleteSpace(str){
  44. let data =[];if(str){
  45. data = str.split(' ');}else{
  46. data = this.txtStr.split(' ');}
  47. let newStr='';for(let i=0;i<data.length;i++){if(data[i]!=''){
  48. newStr+=data[i];}}return newStr
  49. },},};</script><style lang="less" scoped>.tctextarea_box{
  50. width:100%;
  51. height:100%;}.tctextarea{
  52. width:100%;
  53. height:100%;
  54. box-sizing: border-box;
  55. padding:20px;// border: 1px solid #eee;
  56. overflow-y:auto;
  57. display:inline-block;
  58. outline: none;
  59. caret-color:red;
  60. font-size:14px;
  61. color:#323D4D;
  62. line-height:24px;}.tctextarea::-webkit-scrollbar-thumb{
  63. display: block;
  64. cursor: pointer;}</style>

2、父组件调用

  1. /**html**/<div class="lm_textarea_out"><TextareaComponent ref="tcRef" @changeTextareaValue="changeTextareaValue"/></div>/**js**///获取输入内容changeTextareaValue(data){//data为输入的内容}//初始状态
  2. this.$refs.tcRef&&this.$refs.tcRef.createTipHtml();//未查询到数据 显示到输入框内
  3. this.$refs.tcRef&&this.$refs.tcRef.createInputHtml(`未查询到的数据数组`.join('\n'),1);/**css**/.lm_textarea_out{
  4. height:400px;
  5. width:300px;
  6. border-radius:12px;
  7. border:1px solid #eee;}

总结

踩坑路漫漫长@~@


本文转载自: https://blog.csdn.net/weixin_44434938/article/details/128964450
版权归原作者 longlongago~~ 所有, 如有侵权,请联系我们删除。

“vue实现带样式的textarea输入框,contenteditable属性应用”的评论:

还没有评论