繁体   English   中英

如何在JavaScript中的2个单词之间创建空格?

[英]How can i create white space between 2 words in JavaScript?

JavaScript代码

<html>
<head>
<script>
document.write(“Hello There!”);
document.write(“I’m a Boy”);
</script>
</head>
</html>

  • >这里有2个document.write部分。 当显示输出时,我们应该如何在这两个之间创建空白。 就像一行一样,您好! (例如,至少有4个空格)我是男孩

您可以使用&nbsp; 添加空格。

喜欢

<html>
  <head>
   <script>
     document.write("Hello There!");
     document.write("&nbsp;&nbsp;&nbsp;&nbsp;");
     document.write("I’m a Boy");
   </script>
 </head>
</html>

尝试下面的代码-

 document.write("Hello There!"); document.write("\ \ \ \ I'm a Boy"); 

如果要添加换行符,请使用<br /> (不是\\n因为您正在编写HTML):

 document.write("Hello There!"); document.write("<br />"); document.write("I'm a Boy!"); 

如果需要空格,只需写一个空格:

 document.write("Hello There!"); document.write(" "); document.write("I'm a Boy!"); 

最基本的答案是在字符串的末尾添加一个空格:

document.write("Hello There! ");
document.write("Hello There!");

Hello There! Hello There! 将是结果。

如果要使用多个空间,则必须使用牢不可破的空间:

document.write("Hello There!&nbsp;&nbsp;&nbsp;&nbsp;");
document.write("Hello There!");

如果对您来说这只是一个有趣的练习,那很好,但是通常,您不想使用空格进行样式设置。 您应该将文本包装在p或其他html元素中,然后使用css。

您只需要添加&nbsp; 您要在其中添加空间的位置。

<script>
      document.write(“Hello There! &nbsp;&nbsp;&nbsp;&nbsp; I am boy”);
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM