在JavaScript/jQuery中声明具有多行的字符串var [英] Declare a string var with multiple lines in JavaScript/jQuery

查看:616
本文介绍了在JavaScript/jQuery中声明具有多行的字符串var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在jquery中用多行声明一个变量,如

How do i declare a variable in jquery with multiple lines like,

原始变量:

var h = '<label>Hello World, Welcome to the hotel</label><input type="button" value="Visit Hotel"><input type="button" value="Exit">';

我要声明的变量:

var h = '<label>Hello World, Welcome to the hotel</label>
              <input type="button" value="Visit Hotel">
              <input type="button" value="Exit">';

推荐答案

您可以使用\指示该行尚未结束.

You can use \ to indicate that the line has not finished yet.

var h= '<label>Hello World, Welcome to the hotel</label> \
              <input type="button" value="Visit Hotel"> \
              <input type="button" value="Exit">';

注意:当您使用\时,以下行中的空格也将成为字符串的一部分,例如

Note: When you use \, the whitespace in the following line will also be a part of the string, like this

console.log(h);

输出

<label>Hello World, Welcome to the hotel</label>               <input type="button" value="Visit Hotel">               <input type="button" value="Exit">

最好的方法是使用Alien先生在评论部分中建议的方法,将字符串连接起来,像这样

The best method is to use the one suggested by Mr.Alien in the comments section, concatenate the strings, like this

var h = '<label>Hello World, Welcome to the hotel</label>' +
              '<input type="button" value="Visit Hotel">' +
              '<input type="button" value="Exit">';

console.log(h);

输出

<label>Hello World, Welcome to the hotel</label><input type="button" value="Visit Hotel"><input type="button" value="Exit">

这篇关于在JavaScript/jQuery中声明具有多行的字符串var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆