Javascript 在 document.write() 输出中使用换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14808480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:59:31  来源:igfitidea点击:

Use line break in document.write() output

javascript

提问by Muhammad Ali Khamis

How can I use break line in my output? There is something wrong in document.write.

如何在输出中使用换行符?中出现问题document.write

Please review my code and give me best solution..

请检查我的代码并给我最好的解决方案..

<html>
<head>
    <script type="text/javascript">
    function FahToCent (degFah)
    {
        var degCent = new Array ();

        for (var loopCounter = 0;  loopCounter <=2; loopCounter++)
        {
            degCent[loopCounter] = 5/9 * (degFah[loopCounter] - 32);
        }

        return degCent;
    }
    </script>
</head>
<body>
    <script type="text/javascript">
    var degFah = new Array ();

    for (var loopCounter = 0; loopCounter <= 2; loopCounter++)
    {
        degFah[loopCounter] = prompt ("Enter Temperature in Fahrenheit");
    }

    document.write (FahToCent (degFah)+"<br>");
    </script>
</body>
</html>

回答by NPKR

use this

用这个

 function FahToCent (degFah)
{
    return 5/9 * (degFah - 32);;
}

var degFah = new Array ();
for (var loopCounter = 0; loopCounter <= 2; loopCounter++)
{
    degFah[loopCounter] = prompt ("Enter Temperature in Fahrenheit");
    document.write(FahToCent (degFah[loopCounter])+"<br>");   
}

Example Link : http://jsfiddle.net/pradkumar_n/Bucjz/

示例链接:http: //jsfiddle.net/pradkumar_n/Bucjz/

回答by Senthil

just add break tag in your function FahtoCent

只需在您的函数 FahtoCent 中添加中断标记

 degCent[loopCounter] = 5/9 * (degFah[loopCounter] - 32)+"<BR>";

回答by Teemu

You've defined degCentas an Array, which is passed to document.write()as an argument. This argument is supposed to be a string, and now it's an array. Hence it's automatically converted to a string before outputting, using Array.toString()method. This method returns a comma-separated list of array values (as a string).

您已定义degCent为一个数组,它document.write()作为参数传递给。这个参数应该是一个字符串,现在它是一个数组。因此它在输出之前使用Array.toString()方法自动转换为字符串。此方法返回以逗号分隔的数组值列表(作为字符串)。

However, you can convert the returned array to a string, and add linebreaks with the same manouver, using an Array-method called join(). Like this:

但是,您可以将返回的数组转换为字符串,并使用名为 的数组方法使用相同的操作添加换行符join()。像这样:

document.write(FahToCent(degFah).join('<br/>'));

However, document.write()is considered as a bad practise for DOM manipulations. Please check some advanced methods to show the output in the document:

但是,document.write()对于 DOM 操作来说,这被认为是一种不好的做法。请检查一些高级方法以在文档中显示输出:

MDN: innerHTML, MDN: appendChild()

MDN:innerHTML, MDN:appendChild()

回答by gallie

I'm not altogether certain of what you are asking here but I think you just want to go to a new line after the code runs. In which case you need writeline rather than write and remove "br" tag

我不完全确定您在这里问什么,但我认为您只想在代码运行后转到新行。在这种情况下,您需要 writeline 而不是编写和删除“br”标签

document.writeline (FahToCent (degFah));