【问题标题】:Angular 2 click on specific words with spacesAngular 2点击带有空格的特定单词
【发布时间】:2017-11-19 18:48:13
【问题描述】:

我有文本,文本中的某些单词带有空格,例如 我喜欢 R O S E S 和树木。空格是在此功能的帮助下生成的

this.text = this.text.replace(new RegExp(this.words.join('|'), 'g'), function insertSpaces(x) {
   return x.split('').join(' ');
 }); };

文字是I like roses and trees,单词是ROSES,我想让点击单词ROSES成为可能在字母之间,如果我点击那里会出现“-”而不是空格。因此,如果我在 RO 之间单击,如果我在 RO 之间单击,则会出现 RO SES 而不是 ROSES strong>O 和 S,出现 R OS ES 而不是 ROSES 等等……Angular2 可以吗?

更新

我现在试试这段代码

getCaretPos(oField: any, a: any) {
    if (oField.selectionStart || oField.selectionStart === '0') {
      this.caretPos = oField.selectionStart;

//assign char at choosed position to new variable
      a = this.text.charAt(this.caretPos);

//replace this char with "-"
      this.text.replace(a, '-');
    }
  }
}

【问题讨论】:

    标签: javascript angular typescript input


    【解决方案1】:

    此链接演示如何获取当前插入符号位置:http://blog.sodhanalibrary.com/2016/10/get-cursor-position-from-text-area.html#.WURW3GjythE

    然后您可以更改文本以在该字符位置插入破折号。

    这听起来对你有用吗?

    【讨论】:

    • 感谢您的提示.. 它仅适用于 textarea 吗?或者是否也可以与

      一起使用?

    • 我怎样才能在那个字符位置插入破折号?抱歉,我是初学者...刚刚尝试了一个 replace() 函数,但不起作用...必须是 oField.replace('-'); ?...
    • 用您尝试替换的代码更新您的问题。
    【解决方案2】:
    getCaretPos(oField: any, a: any) {
        if (oField.selectionStart || oField.selectionStart === '0') {
          this.caretPos = oField.selectionStart;
          a = this.text.charAt(this.caretPos);
          alert(a);
          this.text = this.text.substring(0, this.caretPos) + '-' + this.text.substring(this.caretPos + 1);
        }
      }
    

    对我有用,希望这对其他人有帮助)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      • 2016-09-12
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多