css切换与css改变

Mal*_*ssi 1 javascript css jquery

我想改变页面的CSS.我看到两种方法:

使用该Jquery .css函数更改html中的每个标记.例如:

$("body").css("background : red")
Run Code Online (Sandbox Code Playgroud)

或者禁用当前的css样式表并启用新的样式表.

例:

function switch_style ( css_title )
{
// You may use this script on your site free of charge provided
// you do not remove this notice or the URL below. Script from
// http://www.thesitewizard.com/javascripts/change-style-sheets.shtml
  var i, link_tag ;
  for (i = 0, link_tag = document.getElementsByTagName("link") ;
    i < link_tag.length ; i++ ) {
    if ((link_tag[i].rel.indexOf( "stylesheet" ) != -1) &&
      link_tag[i].title) {
      link_tag[i].disabled = true ;
      if (link_tag[i].title == css_title) {
        link_tag[i].disabled = false ;
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

哪种方式更有效?或者也许有更好的方法?

Tom*_*rdt 6

还有第三种选择:给出body一个额外的类并body.class2在CSS中使用而不是使用第二个样式表.