对Java来说是全新的。如果可能,希望将这两个脚本合并为一个:
<script>
(function() {
var link = document.createElement('link');
link.rel = "stylesheet";
link.href = "https://www.example1.com";
document.querySelector("head").appendChild(link);
})();
</script>
<script>
(function() {
var link = document.createElement('link');
link.rel = "stylesheet";
link.href = "https://www.example2.com";
document.querySelector("head").appendChild(link);
})();
</script>
答案 0 :(得分:1)
与您的代码相似的脚本
<script>
(function(){
var headUrl = ["https://www.example1.com","https://www.example2.com"];
headUrl.forEach(function(linkURL) {
let link = document.createElement('link');
link.rel = "stylesheet";
link.href = linkURL;
document.querySelector("head").appendChild(link);
})
})()
</script>
答案 1 :(得分:1)
仅具有一个函数,并将变量作为参数传递。您粘贴的两个函数基本上是相同的:
<script>
function oneFunction(href){
var link = document.createElement('link');
link.rel = "stylesheet";
link.href = href;
document.querySelector("head").appendChild(link);
}
oneFunction("https://www.example1.com");
oneFunction("https://www.example2.com");
</script>
答案 2 :(得分:0)
<script>
(function() {
var link1 = document.createElement('link');
var link2 = document.createElement('link');
link1.rel = "stylesheet";
link1.href = "https://www.example1.com";
link2.rel = "stylesheet";
link2.href = "https://www.example2.com";
var head = document.querySelector("head")
head.appendChild(link1);
head.appendChild(link2);
})();
</script>