如何编写JavaScript代码动态向页面head标签中添加外部CSS和JS文件?
- 内容介绍
- 相关推荐
本文共计284个文字,预计阅读时间需要2分钟。
本例展示如何使用JavaScript动态地将外部JS和CSS文件添加到HTML文档的head标签中。以下是一个简化的代码示例:
javascriptfunction appendJQCDN() { var head=document.head || document.getElementsByTagName('head')[0]; var script=document.createElement('script'); script.src=https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js; head.appendChild(script);
var link=document.createElement('link'); link.rel=stylesheet; link.href=path/to/your/css/file.css; head.appendChild(link);}
本文实例讲述了JS实现动态添加外部js、css到head标签的方法。
本文共计284个文字,预计阅读时间需要2分钟。
本例展示如何使用JavaScript动态地将外部JS和CSS文件添加到HTML文档的head标签中。以下是一个简化的代码示例:
javascriptfunction appendJQCDN() { var head=document.head || document.getElementsByTagName('head')[0]; var script=document.createElement('script'); script.src=https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js; head.appendChild(script);
var link=document.createElement('link'); link.rel=stylesheet; link.href=path/to/your/css/file.css; head.appendChild(link);}
本文实例讲述了JS实现动态添加外部js、css到head标签的方法。

