gpt4 book ai didi

javascript - 如何一次追加多个脚本? jQuery

转载 作者:行者123 更新时间:2023-11-28 00:56:54 28 4
gpt4 key购买 nike

var script = document.createElement('script');
script.src = "//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js";
script.type='text/javascript';
var done = false;
script.onload = script.onreadystatechange = function() {

if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
done = true;
promptForUserEntries();
}
};
document.getElementsByTagName("head")[0].appendChild(script);

这会在第一个 Head 中注入(inject)一个脚本。我还有其他三个脚本,并且以相同的方式插入主题,现在我正在寻找如何一次在标签中插入多个脚本。

谢谢

最佳答案

var scripts = ["/path/to/script.js", "/path/to/script2.js"];

for (index = 0; index < scripts.length; ++index) {
var script = document.createElement('script');
script.src = scripts[index];
script.type='text/javascript';
var done = false;
script.onload = script.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
done = true;
//promptForUserEntries();
}
};
document.getElementsByTagName("head")[0].appendChild(script);
}

这将循环遍历要附加的脚本数组,并附加它们

关于javascript - 如何一次追加多个脚本? jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26121046/

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