Welcome 微信登录

首页 / 脚本样式 / JavaScript / JavaScript中字符串拼接的基本方法

非常简单,就用一个"+"将两个字符串"相加":
 var longString = "One piece " + "plus one more piece."; 


      要将多个字符串累积为一个字符串,还可以使用"+="操作符:
 var result = ""; result += "My name is Anders" result += " and my age is 25"; 



   
      要在字符串中添加换行符,需要使用转义字符"":
 var confirmString = "You did not enter a response to the last " +"question.Submit form anyway?"; var confirmValue = confirm(confirmString);



      但这种方法只能用在像警告、确认对话框之类的情况下,如果将这段文本作为HTML内容呈现,就无效了,此时            用"<br>"代替它:
 var htmlString = "First line of string.<br>Second line of string."; document.write(htmlString);

 
      String对象还提供了方法concat(),它完成与"+"相同的功能:
 string.concat(value1, value2, ...)



      不过concat()方法显然不如"+"来得直观简洁。