reactjs - 在 textarea Reactjs 中键入时突出显示文本
问题描述
我需要在 FrontEnd 中执行一个行为,但我不知道该怎么做:在文本区域内,我必须在用户键入时为某些关键字(如“+project”、“@context”)设置背景,就好像它一样是类似于 Regex 测试工具的标记文本。
解决方案
它不是完整的解决方案,但您可以调整此示例:
https://jsfiddle.net/julmot/hdyLpy37/
它使用 markjs 库:
这是javascript代码:
// Create an instance of mark.js and pass an argument containing
// the DOM object of the context (where to search for matches)
var markInstance = new Mark(document.querySelector(".context"));
// Cache DOM elements
var keywordInput = document.querySelector("input[name='keyword']");
var optionInputs = document.querySelectorAll("input[name='opt[]']");
function performMark() {
// Read the keyword
var keyword = keywordInput.value;
// Determine selected options
var options = {};
[].forEach.call(optionInputs, function(opt) {
options[opt.value] = opt.checked;
});
// Remove previous marked elements and mark
// the new keyword inside the context
markInstance.unmark({
done: function(){
markInstance.mark(keyword, options);
}
});
};
// Listen to input and option changes
keywordInput.addEventListener("input", performMark);
for (var i = 0; i < optionInputs.length; i++) {
optionInputs[i].addEventListener("change", performMark);
}
推荐阅读
- dictionary - 在golang中访问映射指针中的对象,取消引用会导致映射副本吗?
- java - 尝试访问最近下载的文件 - Java
- postgresql - 必须先提交新的枚举值,然后才能使用它们
- javascript - 模拟dayjs扩展
- python - 用列表替换数据框中的每个 Null 值
- go - 是否可以使用在其他地方生成的 Metamask 签署交易?
- angular - Spring(Spring Boot)如何向前端提供 CSRF 令牌?
- python-3.x - 在与 Raspberry Pi 连接时为 ADC 设置用户定义的采样率
- r - 在 R 中绘制多个波形图
- python - 比较列表中的两个二维列表,并打印不同的行。但没有一列