在javascript split()之后为子字符串赋予颜色

giving color to substring after javascript split()

本文关键字:字符串 颜色 之后 javascript split      更新时间:2023-09-26

我想用不同的颜色打印在文本区输入的文本,这意味着我用split()方法分隔字符串,效果很好,然后我想用颜色打印文本区中的子字符串,怎么可能

<script type="text/javascript">
function init() {
    document.getElementById('txtarea2').focus();
}
function setcolor() {
var str=document.getElementById('txtarea2').value;
var str1=str.split(":");
var first= str1[0];
var second=str1[1];
document.getElementById('txtarea1').value= first + second;
document.getElementById('txtarea2').focus();
}
</script>
<body onload="init()">
<textarea id="txtarea1" rows="3" cols="20"></textarea>
<textarea id="txtarea2" rows="3" cols="20" onChange="setcolor()"></textarea>
</body>

请帮我

制作一个空div并使用它来附加

<div id="newDiv"></div>

然后创建两个不同的标签并将其附加到此div

first = '<a style="color:red">'+first+'</a>';
second = '<a style="color:blue">'+second+'</a>';
document.getElementById("newDiv").innerHTML=first+second;

你的意思是:


document.getElementById('txtarea1').style.color = 'red';

据我所知,在一个HTML文本区域中不可能像您想要的那样有多种颜色。

文本区域中不能有不同颜色的文本。

要做到这一点,您需要使用富编辑器。

如果文本不可编辑,则使用div/pre输出文本,并使用普通css标记为其着色。