{ const text = '{ "name": "hello"}';-6ren">
gpt4 book ai didi

javascript - 以编程方式设置输入值将包含 "\"

转载 作者:行者123 更新时间:2023-11-30 13:58:37 25 4
gpt4 key购买 nike

我正在尝试将 json 字符串复制到剪贴板:

export const copyToClipboard = () => {
const text = '{ "name": "hello"}';
const selBox = document.createElement('input');
selBox.style.position = 'fixed';
selBox.style.left = '0';
selBox.style.top = '0';
selBox.style.opacity = '0';
selBox.value = JSON.stringify(text);
console.log(text);
console.log(selBox.value);
document.body.appendChild(selBox);
selBox.select();
document.execCommand('copy');
document.body.removeChild(selBox);
};

问题是,selBox 的值中包含字符 \

日志如下所示:

{ "name": "hello"} 这是文本

"{\"name\":\"hello\"}" 这是selBox的值

为什么会发生这种情况,我该如何解决?

最佳答案

变量 text 已经是一个字符串,所以不需要 JSON.stringify()

关于javascript - 以编程方式设置输入值将包含 "\",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56758010/

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