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

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

当前回答

我喜欢这种语法和含义:

string = 'my long string...\n'
       + 'continue here\n'
       + 'and 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');

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

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

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

规则是:当在字符串中时,在需要换行的地方使用;您不必在JavaScript之前或之后放置空格,JavaScript的解释器足够聪明,可以知道不可打印字符表示的长度。

使用脚本标记:

添加<script></script>块,其中包含head标记中的多行文本;按原样获取多行文本…(注意文本编码:UTF-8、ASCII)<脚本>//纯javascriptvar text=document.getElementById(“mySoapMessage”).innerHTML;//使用JQuery的文档做好安全准备$(文档).ready(函数){var text=$(“#mySoapMessage”).html();});</script><script id=“mySoapMessage”type=“text/plain”><soapenv:Envelope xmlns:soapenv=“http://schemas.xmlsoap.org/soap/envelope/“xmlns:typ=”。。。"><soapenv:Header/><soapenv:Body><typ:getConvocatosElement>...</typ:getConvocatosElement></soapenv:Body></soapenv:信封><!-- 此注释将出现在字符串-->中//呃哦,javascript注释。。。SOAP请求将失败</script>

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兔子脸红的消息(直接通过)”+那些施瓦辛格的阴影)!我在哪里?哦,是的,”+'您有一个错误,所有多余的空格都是'+只是肉汁。祝你今天愉快;