Javascript 如何在html中将字符串显示为文本块

Javascript 如何在html中将字符串显示为文本块,javascript,html,Javascript,Html,我有一个返回字符串的JavaScript函数。我想将此字符串写入html页面,但采用文本块格式,可以在其中指定宽度。我目前使用的是只读文本区,但我相信有更好的方法。我该怎么做 例如: 从javascript函数返回的字符串: 那条狗在阳光下懒洋洋的 在html页面上: The dog was lazy in the sun. 编辑: 对不起,我说得不够清楚。 这个javascript函数可以返回任何字符串,我不知道它可以返回什么。 因此,在我的html中,我将执行以下操作: <s

我有一个返回字符串的JavaScript函数。我想将此字符串写入html页面,但采用文本块格式,可以在其中指定宽度。我目前使用的是只读文本区,但我相信有更好的方法。我该怎么做

例如:

从javascript函数返回的字符串: 那条狗在阳光下懒洋洋的

在html页面上:

The dog was  
lazy in the  
sun.
编辑:

对不起,我说得不够清楚。 这个javascript函数可以返回任何字符串,我不知道它可以返回什么。 因此,在我的html中,我将执行以下操作:

<script type="text/javascript" />  
    document.write(functionCall());  
</script>  
但是我想像上面的例子一样格式化它。 不幸的是,用width属性将其包装在标记中不起作用。

将文本放置在
这样,您可以交替地在字符串本身中添加分隔符,并将其添加到标记中,这样它就已经格式化,您就不需要宽度了。

您可以通过像offex所说的那样在div中固定宽度来完成

一张有一定宽度的桌子


谢谢,这正是我想要的。
pre {
    width: 100px;
}
var text, width, container;

width = "200px";        // set this to whatever you like
text = some_function();

// you can create a <div id="text-container"></div> in your
// HTML wherever you want the text to show up, or you can
// the JavaScript just create it at the bottom of the page

container = document.getElementById("text-container");
if(!container) {
    container = document.createElement("div");
    container.id = "text-container";
    document.body.appendChild(container);
}

container.style.width=width;
container.innerHTML = text;