1
我有一個「李」標籤的列表,我可以通過基於用戶輸入的RJS來隱藏和顯示沒有問題,但數據被保存,然後用戶第二天回來,我怎樣才能讓頁面出現元素仍然隱藏?換句話說,根據模型中的數據,我如何告訴一個元素它應該被隱藏起來?如何在頁面加載到rails之前隱藏元素?
我有一個「李」標籤的列表,我可以通過基於用戶輸入的RJS來隱藏和顯示沒有問題,但數據被保存,然後用戶第二天回來,我怎樣才能讓頁面出現元素仍然隱藏?換句話說,根據模型中的數據,我如何告訴一個元素它應該被隱藏起來?如何在頁面加載到rails之前隱藏元素?
所以你用戶點擊一些東西或提交的東西和李項目顯示/隱藏?
然後他們離開網站,也許明天或晚些時候出現?
我會設置一個cookie,其中包含要隱藏的li項目的字符串。
我會有一個jquery函數,我可以將該字符串傳遞給。在我的網站的身體我也有一點像(僞代碼)
if cookie exists {
// echo function in the body tag
echo 'onload="hideItems(' . $_COOKIE['hiddenItems'] . ')";
// so if there's a cookie they'll wind up having code that looks like this
// <body onload="hideItems(12-23-34-52);">
}
你會想這是要分析這些數字,然後隱藏需要隱藏的那些JavaScript函數。
function hideItems(string) {
var partsArray = string.split('-');
// this'll put all your list items in an array
// e.g partsArray[0] == 12 and partsArray[1] == 23
// now with all the items in an array, you should be able to go through and hide any list items that match the values
// e.g
for (i=0; i< {partsArray}.length; i++) {
// jquery hide the list items
$('#listItem'+partsArray[$i]).hide();
}
}
我會想象那麼你的列表中的項目將需要在這樣的格式:
<ul>
<li id="listItem12"></li>
<li id="listItem23"></li>
</ul>
所以我覺得這就是你如何用JavaScript做到這一點。
當然,您可以在服務器端完成它,但在JavaScript甚至加載之前,並將該cookie解析爲紅寶石。如果列表中的項目編號包含在cookie中,則將隱藏的類別回顯到列表項目中。簡單。
我不知道紅寶石,所以我編輯出來的cookie代碼是基於php的行話。
祝你好運。希望這可以幫助。
沒有做到這一點與餅乾,只是實例變量。但這基本上是正確的答案。 – chrishomer 2009-09-27 17:01:43