如何在Javascript连接字符串上强制换行? [英] How to force a line break on a Javascript concatenated string?

查看:162
本文介绍了如何在Javascript连接字符串上强制换行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将变量作为连接字符串发送到文本框,因此我可以在getElementById调用中包含多个变量。

I'm sending variables to a text box as a concatenated string so I can include multiple variables in on getElementById call.

我需要指定换行符,以便正确格式化地址。

document.getElementById("address_box").value = 
(title + address + address2 + address3 + address4);

我已经在换行符之后和变量之后尝试了\ n。并尝试将连接运算符更改为+ =。

I've already tried \n after the line break and after the variable. and tried changing the concatenation operator to +=.

已修复:此问题已解决;

Fixed: This problem was resolved using;

document.getElementById("address_box").value = 
(title + "\n" + address + "\n" + address2 + "\n" +  address3 +  "\n" + address4);

并将文本框从输入类型更改为textarea

推荐答案

你不能在文本框中有多行,你需要一个textarea。然后它在值之间使用 \ n

You can't have multiple lines in a text box, you need a textarea. Then it works with \n between the values.

这篇关于如何在Javascript连接字符串上强制换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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