Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 以编程方式设置输入的值将有\&引用;在里面_Javascript_Html - Fatal编程技术网

Javascript 以编程方式设置输入的值将有\&引用;在里面

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';

我正在尝试将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()