gpt4 book ai didi

javascript - 如何将代码添加到 <script> </script> 中

转载 作者:行者123 更新时间:2023-12-03 06:23:55 30 4
gpt4 key购买 nike

我正在尝试将代码添加到脚本中,但它不起作用

    var script= document.createElement('script');
script.type= 'text/javascript';
script.textContent = var Module = {
TOTAL_MEMORY: 536870912,
errorhandler: null,
compatibilitycheck: null,
dataUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.data",
codeUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.js",
memUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.mem",
};
script.async = true;
document.body.appendChild(script);

如何添加才能变成这样? enter image description here

最佳答案

这边;转义双引号(用 " 括起内部内容),反之亦然。另一种方法是使用数组使代码可读,并使用 .join() 连接它们

    var script= document.createElement('script');
script.type= 'text/javascript';
script.textContent = 'var Module = { TOTAL_MEMORY: 536870912, errorhandler: null, compatibilitycheck: null, dataUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.data", codeUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.js", memUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.mem", };';
script.async = true;
document.body.appendChild(script);

使用数组:

var script= document.createElement('script');

scriptContent = ['var Module = {',
'TOTAL_MEMORY: 536870912,',
'errorhandler: null,',
'compatibilitycheck: null,',
'dataUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.data",',
'codeUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.js",',
'memUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.mem",',
'};',
'console.log(Module)'].join("");

script.type= 'text/javascript';
script.textContent = scriptContent;
script.async = true;
document.body.appendChild(script);

关于javascript - 如何将代码添加到 &lt;script&gt; &lt;/script&gt; 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38739114/

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