我有一个文本框,我在其中放置信息以使用以下 html 和脚本根据该搜索获取新信息:
HTML:
<input name="line" placeholder="Line" onkeydown="if (event.keyCode == 13) { calculate(); return false; }" style="width:40px;" type="textbox">
<input value="Reset" type="reset">
<input id="call" placeholder="Entrepreneur" style="width:95px;" type="textbox">
<input id="number" placeholder="Phonenumber" style="width:110px;" type="textbox">
脚本:
function calculate() {
var line = document.buss.line.value;
var ent;
var num;
switch(line) {
case "12":
case "27":
case "30":
<-- a lot of cases -->
ent = "Entrepreneur";
num = "Phonenumber";
break;
case "42":
case "63":
case "69":
<-- a lot of cases -->
ent = "Entrepreneur";
num = "Number";
break;
default:
}
document.buss.call.value = ent;
document.buss.number.value = num;
}
所有这些都很完美,但现在我需要在文本区域中输入一些新信息,但我无法让它按照我想要的方式打印。我想要的是包含和
的信息,但现在它将其打印为文本而不是 html,有没有办法改变它?还有一种方法可以将其直接打印到页面上,而不是有很多文本框和文本区域?
HTML:
<input name="line" placeholder="Line" onkeydown="if (event.keyCode == 13) { calculate(); return false; }" style="width:40px;" type="textbox">
<input value="Reset" type="reset">
<input id="call" placeholder="Entrepreneur" style="width:114px;" type="textbox">
<input id="number" placeholder="Number" style="width:90px;" type="textbox">
<textarea id="stop" placeholder="Stop" style="width:300px; height:650px"></textarea>
脚本:
function calculate() {
var line = document.buss.line.value;
var ent;
var num;
var stop;
switch(line) {
case "1":
ent = "Entrepreneur";
num = "Number";
stop = "<b>stop1</b><br>stop2<br>stop3<br><b>stop4</b>";
break;
case "2":
ent = "Entrepreneur";
num = "Number";
stop = "<b>stop1</b><br>stop2<br>stop3<br><b>stop4</b>";
break;
default:
}
document.buss.call.value = ent;
document.buss.number.value = num;
document.buss.stop.value = stop;
}
最佳答案
您无法在 Textarea 对象内呈现 html。 如果您不需要文本可编辑,您可以用 div 或 span 标签替换输入和文本区域,并替换其内容。
示例:
如果你替换这个:
<textarea id="stop" placeholder="Stop" style="width:300px; height:650px"></textarea>
这样:
<div id="stop" style="width:300px; height:650px">Stop</div>
并替换它:
document.buss.stop.value = stop;
这样:
document.getElementById('stop').innerHTML = stop;
html 已呈现。
要使重置按钮起作用,您需要添加重置功能。这应该可以解决问题:
<input onclick="resetHtml()" value="Reset" type="reset">
function resetHtml() {
document.getElementById('stop').innerHTML = "Stop";
}
关于javascript - 将 html 作为值发送到 textarea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49354171/