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

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

当前回答

我这样编程:

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');

其他回答

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 x = 1
string = string + `<label class="container">
                       <p>${x}</p>
                   </label>`;

我找到了一个更优雅的解决方案,可能有点与主题无关,因为它使用了PHP,但我相信它对你的一些人来说会很有用和可爱。。。

这个javascript代码应该留在脚本标记中

var html=<?php echo json_encode("

        <div class=container>
            <div class=area1>
                xxx
            </div>
            <div class=area2>
                ".$someVar."
            </div>
        </div>

"); ?>

在输出html中,您将看到类似

var html="\r\n\r\n\t\t\t<div class=container>\r\n\t\t\t\t<div class=area1>\r\n\t\t\t\t\txxx\r\n\t\t\t\t<\/div>\r\n\t\t\t\t<div class=area2>\r\n\t\t\t\t\t44\r\n\t\t\t\t<\/div>\r\n\t\t\t<\/div>\r\n\r\n\t\t";  

 


瞧!,它为您的文件提供了代码可读性。

pD:此示例使用json_encode()PHP函数,但当然也有ASP、Ruby和JSP语言的等效函数。

pD:然而,这个解决方案也有他的局限性,其中之一就是不能在封装的代码中使用javascript变量。

ES6允许您使用反勾号在多行上指定字符串。它被称为模板文字。这样地:

var multilineString = `One line of text
    second line of text
    third line of text
    fourth line of text`;

在NodeJS中使用backtick,Chrome、Firefox、Edge、Safari和Opera都支持它。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

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');

容易的