执行脚本后,我将两个字符串组合在一起,如何使结果行的第一部分变成红色,第二部分变成蓝色
function generateFullName() {
var elem1 = document.getElementById('fName').value;
var elem2 = document.getElementById('lName').value;
document.getElementById('fullName').innerText = elem1 + ' ' + elem2;
}
First Name
<param type="text" id="fName" onkeyup="generateFullName()" class='tsvet' value="2" /><br/> Last Name
<param type="text" id="lName" onkeyup="generateFullName()" value="354" /><br/> Full Name <span id="fullName" class='tsvet' />
<body onload="generateFullName();"> </body>
答案 0 :(得分:3)
使用innerHTML
代替innerText
function generateFullName() {
var elem1 = document.getElementById('fName').value;
var elem2 = document.getElementById('lName').value;
document.getElementById('fullName').innerHTML = '<span style="color:red">' + elem1 + '</span><span style="color:blue"> ' + elem2 + '</span>';
}
First Name
<param type="text" id="fName" onkeyup="generateFullName()" class='tsvet' value="2" /><br/> Last Name
<param type="text" id="lName" onkeyup="generateFullName()" value="354" /><br/> Full Name <span id="fullName" class='tsvet' />
<body onload="generateFullName();"> </body>