当我使用document.execCommand(“copy”)

时间:2015-09-01 05:03:35

标签: javascript

您好我正在使用下面的代码来构建一个字符串并将其复制,但在输出时,当我粘贴它时,换行符不适用

function copyToClipboardShipto() {
        var $temp = $("<input>");

        $("body").append($temp);
        var str1 = "@(Model.firstName)"; var str2 = " "; var str3 = "@(Model.lastName)"; var str4 = "\n";
        var str5 = "@(Model.shiptoes[0].address.address1)";
        var str6 = ",";
        var str7 = "@(Model.shiptoes[0].address.address2)";
        var str8 = "\n";
        var str9 = "@(Model.shiptoes[0].address.city)"; var str10 = ","; var str11 = "@(Model.shiptoes[0].address.state)"; var str12 = "\n";
        var str13 = "@(Model.shiptoes[0].address.zip)";
        var str = str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8 + str9 + str10 + str11 + str12 + str13;
        $temp.val(str).select();
        document.execCommand("copy");
        $temp.remove();
    }
}

firstname lastname223 E JACKSON AVE,city,statezip 任何有帮助的帮助

2 个答案:

答案 0 :(得分:12)

使用<textarea>代替<input>,因为INPUT不支持多行字符串。

var $temp = $("<textarea>");

答案 1 :(得分:2)

使用<textarea>代替<input>不支持换行符。