如何使用jquery加载CSS

Top*_*der 53 html css jquery stylesheet

我在服务器上加载了一个css文件,所以我有一个URL跟我一起.如何使用JQuery在我的perl代码中加载它?

所以目前我在我的梅森页面中硬编码css,页面上缺少这样的东西

JQ.onReady('show', function(){
    JQ.addStyles({styles: ["\n\n.ap_classic { border-top:1px solid #ccc;border-left:1px solid #ccc;border-bottom:1px solid #2F2F1D; border-right:1px solid   #2F2F1D;background-color:#EFEDD4;padding:3px; }  .ap_classic .ap_titlebar { color:#86875D;font-size:12px;padding:0 0 3px 0;line-height:1em; }  .ap_classic .ap_close { float:right; }  .ap_classic .ap_content { clear:both;background-color:white;border:1px solid #ACA976;padding:8px;font-size:11px; } "]});
});
Run Code Online (Sandbox Code Playgroud)

我想避免硬编码这个CSS?

paw*_*wel 76

我不明白为什么你不能只<link/>在该<head/>部分插入元素,但这里是一个jQuery片段:

$('head').append( $('<link rel="stylesheet" type="text/css" />').attr('href', 'your stylesheet url') );
Run Code Online (Sandbox Code Playgroud)

  • document.createStyleSheet( '的style.css'); IE FIX (6认同)

eke*_*ner 42

再次,根据动态加载css样式表不适用于IE.

你需要设置最后的href attr,并且只有在链接元素附加到头部之后:

$('<link>')
  .appendTo('head')
  .attr({
      type: 'text/css', 
      rel: 'stylesheet',
      href: '/css/your_css_file.css'
  });
Run Code Online (Sandbox Code Playgroud)

  • .appendTo('head')也适合我 (2认同)