为什么“<br/>”显示而不是导致休息

时间:2016-03-27 10:25:56

标签: javascript html

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

我正在显示一个文本框

这是html for it

<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>并且它有效。 它是输入类型的东西??

3 个答案:

答案 0 :(得分:2)

文本框的值不会被解释为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,因为你想要显示几行。

答案 1 :(得分:0)

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

<textarea>
this 
is 
some text
</textarea>

请注意,设置textarea.value也会有效。

答案 2 :(得分:0)

使用textarea代替输入类型=&#34; text&#34;添加&#34; cols&#34;和&#34;行&#34;根据您自己的偏好进行属性,然后用&#34; \ n&#34;。

替换br标签
<textarea id="display" cols="15" rows="5" ></textarea>

&#13;
&#13;
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;
&#13;
&#13;
&#13;

相关问题