如何将Javascript作为字符串从HTML传递到函数
我正在尝试使用函数复制一些Javascript代码。其中,从如何将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
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此脚本在哪里执行?我看到一个字符串被复制到剪贴板。没有别的了