[英]How could you combine these two scripts into one?
完全不熟悉Javascript。 如果可能,希望將這兩個腳本合並為一個:
<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>
您的代碼的類似腳本
<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>
僅具有一個函數,並將變量作為參數傳遞。 您粘貼的兩個函數基本上是相同的:
<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>
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.