如何在 JavaScript 中换行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4768118/
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-23 13:59:39  来源:igfitidea点击:

How to break line in JavaScript?

javascripthtml

提问by saurav2109

Please let me know how to break line in JavaScript.

请让我知道如何在 JavaScript 中换行。

<input type='submit' name='Submit' value='Submit' 
onClick="parent.location='mailto:[email protected]?subject=Thanks for writing to me &body=I will get back to you soon. Thanks and Regards Saurav Kumar'">

I want a break line in Subject. The output I need is:

我想要主题中的断线。我需要的输出是:

I will get back to you soon
Thanks and Regards
Saurav Kumar

回答by Oded

Add %0D%0Ato any place you want to encode a line break on the URL.

添加%0D%0A到您要在 URL 上对换行符进行编码的任何位置。

  • %0Dis a carriage return character
  • %0Ais a line break character
  • %0D是回车符
  • %0A是换行符

This is the new line sequence on windows machines, though not the same on linux and macs, should work in both.

这是 windows 机器上的新行序列,虽然在 linux 和 macs 上不一样,但应该适用于两者。

If you want a linebreak in actual javascript, use the \nescape sequence.

如果您想在实际的 javascript 中换行,请使用\n转义序列。



onClick="parent.location='mailto:[email protected]?subject=Thanks for writing to me &body=I will get back to you soon.%0D%0AThanks and Regards%0D%0ASaurav Kumar'

回答by Andreyco

Here you are ;-)

这个给你 ;-)

<script type="text/javascript">
    alert("Hello there.\nI am on a second line ;-)")
</script>

回答by Haresh Narang

I was facing the same problem. For my solution, I added brenclosed between 2 brackets < > enclosed in double quotation marks, and preceded and followed by the + sign:

我面临同样的问题。对于我的解决方案,我添加br了 2 个括号 < > 括在双引号中,并在前面和后面加上 + 符号:

+"<br>"+

+"<br>"+

Try this in your browser and see, it certainly works in my Internet Explorer.

在您的浏览器中试试这个,看看它在我的 Internet Explorer 中确实有效。

回答by mplungjan

alert("I will get back to you soon\nThanks and Regards\nSaurav Kumar");

or %0D%0A in a url

或 %0D%0A 在 url