我想用typescript做一个简单的语法高亮器。有没有办法在给定索引的文本中插入css?假设我有一个文本区域,我想让0到10的文本颜色变成红色,这可能吗?
uxh89sit1#
const textarea = document.getElementById('yourTextareaId') as HTMLTextAreaElement; function applyHighlight(start: number, end: number, color: string) { const before = textarea.value.substring(0, start); const highlighted = `<span style="color: ${color}">${textarea.value.substring(start, end)}</span>`; const after = textarea.value.substring(end); textarea.innerHTML = before + highlighted + after; } // forExsmple applyHighlight(0, 10, 'red');
字符串
1条答案
按热度按时间uxh89sit1#
字符串