我在Ruby中有以下代码。我想把这段代码转换成JavaScript。JS中的等效代码是什么?

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

当前回答

2015年更新:现在已经过了六年:大多数人使用模块加载器,主要模块系统都有加载模板的方法。它不是内联的,但最常见的多行字符串类型是模板,无论如何,模板应该被排除在JS之外。

require.js:'需要文本'。

使用require.js“text”插件,在template.html中使用多行模板

var template = require('text!template.html')

NPM/browserify:“brfs”模块

Browserify使用“brfs”模块加载文本文件。这实际上会将模板构建到捆绑的HTML中。

var fs = require("fs");
var template = fs.readFileSync(template.html', 'utf8');

容易的

其他回答

我很惊讶我没有看到这一点,因为它在我测试过的任何地方都有效,并且对模板非常有用:

<script type="bogus" id="multi">
    My
    multiline
    string
</script>
<script>
    alert($('#multi').html());
</script>

有人知道有HTML但不起作用的环境吗?

ES6更新:

正如第一个答案所提到的,使用ES6/Babel,您现在只需使用反引号即可创建多行字符串:

const htmlString = `Say hello to 
multi-line
strings!`;

内插变量是一个流行的新功能,带有反勾分隔字符串:

const htmlString = `${user.name} liked your post about strings`;

这只是将文件转换为串联:

user.name + ' liked your post about strings'

ES5原始答案:

谷歌的JavaScript风格指南建议使用字符串连接而不是转义换行:不要这样做:var myString='相当长的英文文本字符串,错误消息\事实上,这是一个错误\让精力充沛的兔子脸红的信息\那些施瓦辛格的阴影)!我在哪里?哦,是的\你有一个错误,所有多余的空格都是\只是肉汁。祝你今天愉快;在编译时,不能安全地删除每行开头的空白;斜杠后的空白将导致棘手的错误;虽然大多数脚本引擎都支持这一点,但它不是ECMAScript的一部分。请改用字符串串联:var myString='相当长的英文文本字符串,错误消息'+“事实上,这是一个错误”+“让Energizer兔子脸红的消息(直接通过)”+那些施瓦辛格的阴影)!我在哪里?哦,是的,”+'您有一个错误,所有多余的空格都是'+只是肉汁。祝你今天愉快;

你可以这样做。。。

var string = 'This is\n' +
'a multiline\n' + 
'string';

这适用于IE、Safari、Chrome和Firefox:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<div class="crazy_idea" thorn_in_my_side='<table  border="0">
                        <tr>
                            <td ><span class="mlayouttablecellsdynamic">PACKAGE price $65.00</span></td>
                        </tr>
                    </table>'></div>
<script type="text/javascript">
    alert($(".crazy_idea").attr("thorn_in_my_side"));
</script>

我这样编程:

sys = {
    layout: null,
    makeLayout: function (obCLS) {
        this.layout = $('<div />').addClass('editor').appendTo($(obCLS)).append(

            /* Cargador */
            /* @this.layout.find('> div:nth-of-child(1)'); */
            '<div>' +
            '   <p>Seleccione la imagen que desea procesar.</p>' +
            '   <input type="button" value="Seleccionar" class="btn btn-xlarge btn-success" />' +
            '   <span></span>' +
            '</div>' +

            /* Cargador - Progreso */
            /* @this.layout.find('> div:nth-of-child(2)'); */
            '<div>' +
            '   <div>' +
            '       <div></div>' +
            '       <div>' +
            '           <div></div>' +
            '       </div>' +
            '   </div>' +
            '</div>' +

            /* Editor */
            /* @this.layout.find('> div:nth-of-child(3)');
             * @sidebar = this.layout.find('> div:nth-of-child(3) > div > div > div:nth-of-type(1)');
             * @body    = this.layout.find('> div:nth-of-child(3) > div > div > div:nth-of-type(2) > div'); */
            '<div>' +
            '   <div>' +
            '       <div>' +
            '           <div></div>' +
            '           <div>' +
            '               <div></div>' +
            '           </div>' +
            '       </div>' +
            '   </div>' +
            '</div>'
        );
    }
}

sys.makeLayout('#div');