angular - 如何在单击事件中从字符串中选择“单词”
问题描述
我有一个字符串,当我单击字符串中的任何单词时,它应该从该单击的单词中给出另一个字符串,然后返回到字符串的开头,即
str = '这是一个测试字符串。' 单击“测试”一词后,新字符串应类似于“这是一个测试”等。
目前,当我选择整个单词时,它会给出预期的结果,但不会在 'click' 上给出。
此外,在以下链接中,他们提供了解决方案,但它在 jquery 中,我想在Anguar 10中实现
app.component.html
<textarea class="form-control rows=10 cols=50 cstm-textarea" (click)="TargetPrefix($event)">
This is a test string
</textarea>
app.component.ts
TargetPrefix(event) {
const start = event.target.selectionStart;
const end = event.target.selectionEnd;
const result = event.target.value.substr(end - start, start);
console.log("the clicked word in the string", result);
}
[在此处输入图像描述][1] [在此处输入图像描述][2]
谢谢
onClick [1]:https ://i.stack.imgur.com/qjwKO.jpg
解决方案
在这里,您有一个如何以角度实现它的示例
https://stackblitz.com/edit/angular-ivy-prcewk?file=src%2Fapp%2Fapp.component.ts
我刚刚更新了你的targetPrefix
方法。
推荐阅读
- json - 如何使用 pandas 读取 JSON 数据?
- stm32 - 通过 ESP8266 WiFi 使 STM32 上的 FatFS 文件系统可用于 PC
- xml - 在 XML 中添加带空格的行
- django - 是否可以使用 django orm 生成结果集的哈希?
- javascript - 以特定顺序将图像绘制到画布
- kotlin - 如何更改解析?
- phpstorm - 在 PhpStorm 中设置默认包含路径
- node.js - Angular 6 和 PWA
- python-3.x - 如何根据元素的频率对熊猫数据框进行颜色编码?
- google-apps-script - 谷歌数据工作室表显示日期列为空