gpt4 book ai didi

javascript - encodeURIComponent() 忽略换行符

转载 作者:行者123 更新时间:2023-11-30 11:09:40 33 4
gpt4 key购买 nike

我正在尝试对来自文本区域的字符串进行编码,以便我可以为 mailto 链接输出编码结果。空格和特殊字符的编码很好,但换行符不是,它们只是被编码为空格。 如何对我的换行符进行编码?

if (id == 'subject' || id == 'body') {
let str = output.innerText;
str = encodeURIComponent(str);
// str = str.replace(/\n/g, "%0A").replace(/ /g, "%20").replace(/&/g, "%26");
output.innerText = str;
}

我也试过这个也不管用:

str = encodeURIComponent(str).replace(/\n/g, '%0D%0A');

这是我的输出当前的样子:

mailto:mckeever02@gmail.com?body=Testing%20Should%20be%20a%20line%20break%20before%20this%20sentence

请注意,换行符只是被编码为 %20(空格)。有什么想法吗?

最佳答案

换行符替换为 %0A。使用 value 而不是 innerHTML/innerText

从 textarea 获取文本

const ta = document.getElementById("ta");
const val = ta.value;

console.log(encodeURIComponent(val));
<textarea id="ta">
First line
Second line
</textarea>

关于javascript - encodeURIComponent() 忽略换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54216400/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com