为什么“<br>"显示而不是引起中断

Why is "<br>" displaying instead of causing a break

本文关键字:中断 quot lt br gt 为什么 显示      更新时间:2023-09-26

有人问我这个问题,我很抱歉,但我没有看到问题的答案。我试图将结果显示在文本框中,当我使用"<br>"时,它实际上是在写br,而不是创建换行

我正在向文本框显示

这是它的html

<input type ="text" name="display" id="display"  class="display" readonly>

这是我使用的javascript

document.getElementById("display").value = "The highest test score is: " + max  + "<br>"+
   "The lowest test score is: " + min + "<br>" +
   "The average test score is: " + avg + "<br>" +
   "The scores are: " + results;

我以前使用过这个,但我已经向<p>显示了它,它起了作用。是输入类型的东西吗??

文本框的值不会被解释为HTML。

这里的解决方案是使用'n:

document.getElementById("display").value = "The highest test score is: " + max  + "'n"+
   "The lowest test score is: " + min + "'n" +
   "The average test score is: " + avg + "'n" +
   "The scores are: " + results;

但是您可能应该在这里使用textarea而不是input,因为您想要显示几行。

<input type="text" />只能包含单行文本,您应该使用textarea元素,它可以包含多行,如下所示:

<textarea>
this 
is 
some text
</textarea>

请注意,设置textarea.value也会起作用。

使用textarea而不是input type="text"根据自己的喜好添加"cols"answers"rows"属性,然后将br标记替换为"''n"。

<textarea id="display" cols="15" rows="5" ></textarea>

document.getElementById("display").value = "The highest test score is: " + max  + "'n"+
   "The lowest test score is: " + min + "'n" +
   "The average test score is: " + avg + "'n" +
   "The scores are: " + results;