如何在javascript中提供空间或下一行功能?

时间:2014-04-21 10:35:25

标签: javascript html

这是我的java脚本代码:

function for_approve(){
var approve = confirm(format_stock_id.length + 
               "&nbsp;Stock Record(s) is selected for approval.<br /> 
               Do You Want to send it for Approval");

}

它给我结果:

3 &nbsp;Stock Record(s) is selected for approval.<br />Do You Want to send it for Approval

我应该怎样做空间和新线。

5 个答案:

答案 0 :(得分:3)

使用\n符号:

function for_approve(){
var approve = confirm(format_stock_id.length + 
               " Stock Record(s) is selected for approval.\n 
               Do You Want to send it for Approval");

}

你可以在这里查看。在浏览器中打开控制台并写下:alert("bob\nbob");

示例

命令:

command

结果:

example

P.S。:同样的空间。您不必使用转义查询&nbsp;。只需使用(简单空格)。

答案 1 :(得分:0)

要跳转到下一行,您需要使用\ n而对于空格,无需编写&amp; nbsp,只需编写空白字符,


function for_approve(){
var approve = confirm(format_stock_id.length + " Stock Record(s) is selected for approval.\n Do You Want to send it for Approval");
}

答案 2 :(得分:0)

您可以在字符串中使用JS Escape字符(参考:http://www.w3schools.com/js/default.asp):

\'  single quote
\"  double quote
\\  backslash
\n  new line
\r  carriage return
\t  tab
\b  backspace
\f  form feed

答案 3 :(得分:0)

如果你想让句子对复数有点整洁,你可以这样做:

if (format_stock_id.length == 1) {
    var approve = confirm("1 Stock Record is selected for approval.\n 
    Do You Want to send it for Approval");
} else {
    var approve = confirm(format_stock_id.length + " Stock Records are selected for approval.\n
    Do You Want to send them for Approval");
}

这样的小事在最终产品中会产生很大的差异。

答案 4 :(得分:0)

使用:

\xa0

空间

\n

新行

您的代码将是:

function for_approve(){
   var approve = confirm(format_stock_id.length + "\xa0 Stock Record(s) is selected for approval.\n Do You Want to send it for Approval");
}