Home > Web Front-end > JS Tutorial > body text

Javascript dynamic loading css method summary_javascript skills

WBOY
Release: 2016-05-16 18:50:10
Original
1011 people have browsed it
1. Used to load necessary files in external CSS files
@importurl(style.css);
//Can only be used in CSS files or style tags
2. Simply load an external CSS file into the page
document.createStyleSheet(cssFile);
2. Use the createElement method to create a CSS Link tag
varhead=document.getElementsByTagName('HEAD ').item(0);
varstyle=document.createElement('link');
style.href='style.css';
style.rel='stylesheet';
style .type='text/css';
head.appendChild(style);
The following are two functions that are often used.
Copy code The code is as follows:

functionloadJs(file){
varscriptTag=document.getElementById('loadScript');
varhead=document.getElementsByTagName(' head').item(0);
if(scriptTag)head.removeChild(scriptTag);
script=document.createElement('script');
script.src="../js/ mi_" file ".js";
script.type='text/javascript';
script.id='loadScript';
head.appendChild(script);
}
functionloadCss (file){
varcssTag=document.getElementById('loadCss');
varhead=document.getElementsByTagName('head').item(0);
if(cssTag)head.removeChild(cssTag) ;
css=document.createElement('link');
css.href="../css/mi_" file ".css";
css.rel='stylesheet';
css.type='text/css';
css.id='loadCss';
head.appendChild(css);
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!