我可以将此字符串设为多行吗?

时间:2013-06-27 16:33:04

标签: javascript html5

我可以将以下javascript变量设为多行吗?

var newPage = $('<div data-role="page" id="link'+myvar+'" data-url="link'+myvar+'"><div data-role="header" data-position="fixed"><a href="#" data-rel="back" data-icon="back">Back</a></div><iframe src="http://docs.google.com/viewer?embedded=true&url=http://www.example.com'+myarrayA[myvar]+'" style="border: 0; position:absolute; top:0; left:0; right:0; bottom:0; width:100%; height:100%; background: white"></iframe></div>');

3 个答案:

答案 0 :(得分:3)

有些方法可以在js中模拟多行字符串:

  • 普通的旧字符串连接方法
var string = "first line " + 
             "second line " + 
             //...
             "nth line"; 
  • 数组连接方法
var string = ["first line ", 
             "second line ", 
             //...
             "nth line"].join(''); 
var string = "first line \ 
             second line \ 
             //...
             nth line"; 

我个人喜欢 join方法,因为它是有效的javascript,并且您可以在行之间插入字符。例如,如果您想将导出您的字符串作为多行html段落,您可以轻松地像这样做:

var pContent = ["first line ", 
                "second line "].join('<br>');

答案 1 :(得分:0)

是的,你可以!

var a = $('<div data-role="page" id="link'
+myvar+'" data-url="link'+myvar+'"><div data-role="header" \
 data-position="fixed"><a href="#" data-rel="back" \
data-icon="back">Back</a></div> \
<iframe src="http://docs.google.com/viewer? /embedded=true&url=http://www.example.com'
+myarrayA[myvar]+'" style="border: 0; position:absolute; \
top:0; left:0; right:0; bottom:0; width:100%; height:100%; \
background: white"></iframe></div>');

答案 2 :(得分:0)

...是

var newPage = $('<div data-role="page" id="link'+myvar+'" data-url="link'+myvar+'">' +
  '  <div data-role="header" data-position="fixed">' +
  '    <a href="#" data-rel="back" data-icon="back">Back</a>' +
  '  </div>' +
  '  <iframe src="http://docs.google.com/viewer?embedded=true&url=http://www.example.com'+myarrayA[myvar]+'" style="border: 0; position:absolute; top:0; left:0; right:0; bottom:0; width:100%; height:100%; background: white"></iframe>' +
  '</div>');

请参阅:http://jsfiddle.net/4GgTC/