2013-03-17 79 views
0
<script type="text/javascript"> 
function changeStyle(title) { 
var lnks = document.getElementsByTagName('link'); 
for (var i = lnks.length - 1; i >= 0; i--) { 
if (lnks[i].getAttribute('rel').indexOf('style')> -1 && lnks[i].getAttribute('title')) { 
lnks[i].disabled = true; 
if (lnks[i].getAttribute('title') == title) lnks[i].disabled = false; 
}}} 
function getActiveStyleSheet() { 
var i, a; 
for(i=0; (a = document.getElementsByTagName("link")); i++) { 
    if(a.getAttribute("rel").indexOf("style") != -1 
    && a.getAttribute("title") 
    && !a.disabled) return a.getAttribute("title"); 
    } 
    return null; 
} 
</script> 

我上面的代碼,我需要我怎麼會使用getActiveStyleSheet功能在cookie或HTML5 localStorage的,我可以在每個窗口調用儲存顯示幫助用戶風格偏好。我真的不知道我會如何實現這一點,如果任何人都可以幫助它將不勝感激。javascript函數值存儲在cookie

回答

0

可以在localStorage的設置一些數據,像這樣:

localStorage.setItem(key, value)

如果你想存儲在localStorage的你getActiveStyleSheet調用的結果,你能做到這一點是這樣的:

// set the active stylesheet in localstorage 
localStorage.setItem('activeStylesheet', getActiveStyleSheet()) 

// retrieve the data from localStorage 
localStorage.getItem('activeStylesheet') 

你可以用cookies做類似的事情。這裏是如何寫的cookie一些文檔:https://developer.mozilla.org/en-US/docs/DOM/document.cookie#Writing_a_cookie

還有一些進一步閱讀的localStorage:https://developer.mozilla.org/en-US/docs/DOM/Storage#localStorage

+0

感謝Freyday - 我similiar嘗試過的東西,但它仍然無法正常工作。我在