Javascript 以编程方式设置输入的值将有\&引用;在里面
我正在尝试将json字符串复制到剪贴簿:Javascript 以编程方式设置输入的值将有\&引用;在里面,javascript,html,Javascript,Html,我正在尝试将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';
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()