JS動態修改網站圖標以及標題


 1 const changeFavicon = link => {
 2   let $favicon = document.querySelector('link[rel="icon"]');
 3   // If a <link rel="icon"> element already exists,
 4   // change its href to the given link.
 5   if ($favicon !== null) {
 6     $favicon.href = link;
 7     // Otherwise, create a new element and append it to <head>.
 8   } else {
 9     $favicon = document.createElement("link");
10     $favicon.rel = "icon";
11     $favicon.href = link;
12     document.head.appendChild($favicon);
13   }
14 };
15 let icon = ''; // 圖片地址
16 changeFavicon(icon); // 動態修改網站圖標
17 let title = ''; // 網站標題
18 document.title = title; // 動態修改網站標題

 將此文件導入到 index.html 文件中即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM