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