Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.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作为字符串从HTML传递到函数_Javascript_Html - Fatal编程技术网

如何将Javascript作为字符串从HTML传递到函数

如何将Javascript作为字符串从HTML传递到函数,javascript,html,Javascript,Html,我正在尝试使用函数复制一些Javascript代码。其中,从HTMLcode触发 它不起作用,因为出现的双引号script标记关闭了onclick属性 函数copytext(数据){ var tempInput=document.createElement(“输入”); tempInput.setAttribute('value',数据); document.body.append(tempInput); tempInput.select(); 文件。执行命令(“副本”); tempInput

我正在尝试使用函数复制一些Javascript代码。其中,从
HTML
code触发

它不起作用,因为出现的双引号
script
标记关闭了
onclick
属性

函数copytext(数据){
var tempInput=document.createElement(“输入”);
tempInput.setAttribute('value',数据);
document.body.append(tempInput);
tempInput.select();
文件。执行命令(“副本”);
tempInput.remove();
}

引号未正确转义

函数copytext(数据){
控制台日志(数据);
var tempInput=document.createElement(“输入”);
tempInput.setAttribute('value',数据);
document.body.append(tempInput);
tempInput.select();
文件。执行命令(“副本”);
tempInput.remove();
}

您需要转义引号

<button onclick='copytext("<script src=\"https://code.jquery.com/jquery-3.3.1.min.js\"></script>")'>Copy</button>
复制
或者在较新的浏览器中使用模板文本

<button onclick="copytext(`<script src='https://code.jquery.com/jquery-3.3.1.min.js'></script>`)">Copy</button>
复制
试试这个

函数copytext(数据){
控制台日志(数据);
var tempInput=document.createElement(“输入”);
tempInput.setAttribute('value',数据);
document.body.append(tempInput);
tempInput.select();
文件。执行命令(“副本”);
tempInput.remove();
}

aaaa
以下是一种可能的解决方案,但尚未经过测试

#< script id="scriptTagId" type=''... #>
document.scripts.namedItem("scriptTagId")
#
document.scripts.namedItem(“scriptTagId”)

@Seblor我看不到任何评估为什么要在函数中加载脚本???@Dr.skill可能是一个按钮,上面写着“将jQuery脚本标记复制到剪贴板以插入页面”@Seblor此脚本在哪里执行?我看到一个字符串被复制到剪贴板。没有别的了