首页 > 技术文章 > textarea 根据光标位置添加内容

hpx2020 2019-09-05 17:50 原文

 // 获取焦点
      let txt = document.getElementById("countRule");
      let temp = txt.value;
      txt.focus();
      let pointIndex = txt.selectionStart;
      let str1 = temp.substr(0,pointIndex);
      let str2 = temp.substr(pointIndex,temp.length);
      txt.value = str1+dom+str2;

      if( site != 'no' ){
        let newIndex = pointIndex + site;
        txt.setSelectionRange(newIndex,newIndex);
      }

  

推荐阅读