【问题标题】:How to insert an intro in console.log() in Javascript如何在 Javascript 中的 console.log() 中插入介绍
【发布时间】:2017-05-09 02:11:42
【问题描述】:

我想使用 console.log() 打印一些文本。我有几行,每行我使用了一个console.log()。我想创建 2 个段落。我怎么能做到?谢谢

	console.log("1 : Lister les contacts"); //paragraph 1
	console.log("2 : Ajouter un contact");  //paragraph 1
	console.log("0 : Quitter");  //paragraph 1
	console.log("Choisissez une option :")); //paragraph 2
	
	

【问题讨论】:

  • 您希望它只使用两次 console.log 来完成吗?是这个问题吗?
  • “段落”是什么意思?你问如何打印一个空行?
  • 你将如何阅读并采取行动?

标签: javascript console.log paragraph


【解决方案1】:

console.log("1 : Lister les contacts"); //paragraph 1
console.log("2 : Ajouter un contact"); //paragraph 1
console.log("0 : Quitter"); //paragraph 1
console.log(""); //paragraph 1
console.log("Choisissez une option :"); //paragraph 2

或者您可以使用\n 换行并使用两次\n\n 换行:

console.log("   p1 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \n\n   p2 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. ");

【讨论】:

    【解决方案2】:

    \n 是一个新行,\t 是一个制表符(缩进)。此外,您可以“连接”字符串(基本上是将它们加在一起。

    var paragraph1 = "1 : Lister les contacts\n";
    paragraph1 = paragraph1 + "2 : Ajouter un contact\n";
    paragraph1 = paragraph1 + "0 : Quitter\n");
    var paragraph2 = "Choisissez une option :";
    
    console.log(paragraph1 + "\n" + paragraph2);
    

    这将导致您显示的内容没有缩进,并且段落之间有一条线。注意:单独的 console.log() 调用总是在不同的行上,因此将它们保存在单独的控制台日志中,您最后不需要 \n

    此外,您可以在列表中每一行的开头添加一个\t 以帮助将其分开。

    【讨论】:

      【解决方案3】:

      console.log("1 : Lister les contacts"); //paragraph 1
      console.log("2 : Ajouter un contact");  //paragraph 1
      console.log("0 : Quitter");  //paragraph 1
      console.log("\n\nChoisissez une option :"); //paragraph 2​

      【讨论】:

        【解决方案4】:

        您可以将模板文字用于多行字符串。模板文字由反引号分隔:

        console.log(`1 : Lister les contacts
        2 : Ajouter un contact  
        0 : Quitter
                 
                 `);   //paragraph 1
                 
        console.log('Choisissez une option :');   //paragraph 2

        【讨论】:

        • 为了完整起见,请注意它不适用于某些较旧的浏览器。例如。 IE11
        • @yezzz 同意了。无论如何,控制台日志适用于开发人员,旧版浏览器支持在这里不是问题。 :D
        【解决方案5】:

        您可以在两行之间使用\r\n 添加新行,以便调用console.log 打印前3 行。

        //paragraph 1
        console.log("1 : Lister les contacts\r\n2 : Ajouter un contact\r\n0 : Quitter");
        //paragraph 2
        console.log("Choisissez une option :"); //paragraph 2

        您在最后一个 console.log() 上还有一个额外的 )

        【讨论】:

          【解决方案6】:

          我会说法语,我认为你想要这样的东西。你就是不知道怎么解释。

          <form role="form" class="form-horizontal">
              <fieldset>
                <div class="form-group">
                  <label class="label">Choisissez une option :</label>
                  <div class="select">
                      <select class="myoptions">
                          <option value="1">1 : Lister les contacts</option>
                          <option value="2">2 : Ajouter un contact</option>
                          <option value="3">0 : Quitter</option>
                      </select>
                  </div>
                </div>
              </fieldset>
          </form>

          【讨论】:

          • 没有。这是一个不同的问题。
          • 问题是关于 console.log() 而不是 HTML 表单。我认为这与语言无关。顺便说一句,你的英语写得很完美(y)
          • @Abhi Andhariya - 谢谢 Abhi。我的英语很好,因为我住在美国。我用法语教授 Javascript,并多次回答过类似的问题。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-05-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多