Home >Web Front-end >JS Tutorial >Detailed explanation of usage examples for creating link tags using js and jquery

Detailed explanation of usage examples for creating link tags using js and jquery

伊谢尔伦
伊谢尔伦Original
2017-07-21 09:27:094741browse

I believe that 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 link tags should look like this:

var cssURL = '/style.css',
    linkTag = $(&#39;<link href="&#39; + cssURL + &#39;" rel="stylesheet" type="text/css" media="&#39; + (media || "all") + &#39;" charset="&#39;+ charset || "utf-8" +&#39;" />&#39;);
// 请看清楚,是动态将link标签添加到head里   
$($(&#39;head&#39;)[0]).append(linkTag);

Use native JavaScript to create link tags

If you like pure natural JavaScript, you need to write like this:

var head = document.getElementsByTagName(&#39;head&#39;)[0],
    cssURL = &#39;/style.css&#39;,
    linkTag = document.createElement(&#39;link&#39;);
 
    linkTag.id = &#39;dynamic-style&#39;;
 linkTag.href = cssURL;
 linkTag.setAttribute(&#39;rel&#39;,&#39;stylesheet&#39;);
 linkTag.setAttribute(&#39;media&#39;,&#39;all&#39;);
 linkTag.setAttribute(&#39;type&#39;,&#39;text/css&#39;);
 
head.appendChild(linkTag);

Special method in IE createStyleSheet

The createStyleSheet method unique to IE is also very convenient.

var head = document.getElementsByTagName(&#39;head&#39;)[0],
    cssURL = &#39;themes/BlueNight/style.css&#39;,
 // document.createStyleSheet 的同时就已经把link标签添加到了head中了,怎么讲呢,倒是挺方便
    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.

Basically the introduction is over, let’s take a look at the complete solution:

function createLink(cssURL,lnkId,charset,media){ 
var head = $($(&#39;head&#39;)[0]),
    linkTag = null;
 
 if(!cssURL){
     return false;
 }
 
 linkTag = $(&#39;<link href="&#39; + cssURL + &#39;" rel="stylesheet" type="text/css" media="&#39; + (media || "all") + &#39;" charset="&#39;+ charset || "utf-8" +&#39;" />&#39;);
  
 head.append(linkTag);
}
function createLink(cssURL,lnkId,charset,media){ 
    var head = document.getElementsByTagName(&#39;head&#39;)[0],
        linkTag = null;
  
 if(!cssURL){
     return false;
 }
    
 linkTag = document.createElement(&#39;link&#39;);
 linkTag.setAttribute(&#39;id&#39;,(lnkId || &#39;dynamic-style&#39;));
 linkTag.setAttribute(&#39;rel&#39;,&#39;stylesheet&#39;);
 linkTag.setAttribute(&#39;charset&#39;,(charset || &#39;utf-8&#39;));
 linkTag.setAttribute(&#39;media&#39;,(media||&#39;all&#39;));
 linkTag.setAttribute(&#39;type&#39;,&#39;text/css&#39;);
    linkTag.href = cssURL; 
 
    head.appendChild(linkTag); 
}


The above is the detailed content of Detailed explanation of usage examples for creating link tags using js and jquery. For more information, please follow other related articles on the PHP Chinese website!

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