Home >
Article > Web Front-end > How to dynamically create link tags in the head with JavaScript_javascript skills
How to dynamically create link tags in the head with JavaScript_javascript skills
WBOYOriginal
2016-05-16 16:25:242018browse
The example in this article describes the method of dynamically creating link tags in the head using JavaScript. Share it with everyone for your reference. The specific analysis is as follows:
I believe many front-end friends have encountered the need to use JavaScript to dynamically create style sheet tags - link tags. Here we will talk about how to dynamically create link tags in the browser.
Use jQuery to create link tags
If you like to use jQuery in development, then using jQuery to create the link tag should look like this:
var head = document.getElementsByTagName('head')[0],
cssURL = 'themes/BlueNight/style.css',
// document.createStyleSheet The link tag has been added to the head at the same time. How to say, it is quite convenient
LinkTag = document.createStyleSheet(cssURL);
The createStyleSheet([sURL] [, iIndex]) method accepts two parameters, sURL is the URL path of the CSS file. iIndex is an optional parameter, which refers to the index position of the inserted link in the stylesheets collection on the page. The default is to add the newly created style at the end.
Complete solution
Basically the introduction is over, let’s take a look at the complete solution:
I hope this article will be helpful to everyone’s JavaScript programming design.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn