我想用不同的颜色打印在textarea中输入的文本意味着我用split()方法分隔字符串,它工作正常然后我想用颜色打印textarea中的子字符串怎么可能
<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;
关于javascript - javascript split() 之后为子字符串赋予颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9562380/