2011-01-27 52 views
0

與test1.css更換test.css假設我有一個像 我怎麼能的jQuery與test1.css替換test.css css文件引用。幫助使用代碼片段。如何通過jQuery的

感謝

+0

這個問題可以用更詳細一點 – jAndy 2011-01-27 19:24:19

+0

請包括你的代碼表明你已經付出了一些努力。 – krishna 2011-01-27 19:35:01

回答

1
function replaceStyle(old, _new) { 
    var ns = $('<style>', { 
     href: _new, 
     type: 'text/css', 
     rel: 'stylesheet' 
    }); 

    $.each(document.styleSheets, function(i, style) { 
     if(style.href && style.href.indexOf('all.css') > -1) { 
      $(style.ownerNode).replaceWith(ns); 
     } 
    });  
} 

這應該這樣做。你可以使用這樣的:

replaceStyle('app.css', '/mypath/css/new.css'); 
0

那麼這裏是你如何可以有條件地包括一個樣式表或其它。

if (something_is_checked_here) { 
    $("head").append('<link href="test1.css" rel="stylesheet" type="text/css">'); 
} else { 
    $("head").append('<link href="test.css" rel="stylesheet" type="text/css">'); 
} 
1

做到這一點:

originalCss = $('link[href*="test.css"]').attr('href'); 
newCss = originalCss.replace('test.css', test1.css'); 

$('link[href*="test.css"]').attr('href', newCss); 

編輯(或在這樣的功能):

function replaceCss(oldCss, newCss) { 

    originalHref = $('link[href*=' + oldCss + ']').attr('href'); 
    newHref = originalHref.replace(oldCss, newCss) 
    $('link[href*=' + oldCss + ']').attr('href', newHref); 

    }