℡Wang Yan 2009-04-30 02:11 采纳率: 100%
浏览 583
已采纳

在 JavaScript 中创建多行字符串

I have the following code in Ruby. I want to convert this code into JavaScript. what's the equivalent code in JS?

text = <<"HERE"
This
Is
A
Multiline
String
HERE

转载于:https://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript

  • 写回答

30条回答

  • 撒拉嘿哟木头 2009-04-30 02:15
    关注

    Update:

    ECMAScript 6 (ES6) introduces a new type of literal, namely template literals. They have many features, variable interpolation among others, but most importantly for this question, they can be multiline.

    A template literal is delimited by backticks:

    var html = `
      <div>
        <span>Some HTML here</span>
      </div>
    `;
    

    (Note: I'm not advocating to use HTML in strings)

    Browser support is OK, but you can use transpilers to be more compatible.


    Original ES5 answer:

    Javascript doesn't have a here-document syntax. You can escape the literal newline, however, which comes close:

    "foo \
    bar"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(29条)

报告相同问题?

悬赏问题

  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决