Jquery 如何在页面加载到rails之前隐藏元素?

Jquery 如何在页面加载到rails之前隐藏元素?,jquery,ruby-on-rails,Jquery,Ruby On Rails,我有一个“li”标签列表,我可以根据用户输入通过RJS隐藏和显示没有问题,但是数据保存了什么,然后用户第二天回来,我如何使页面中的元素仍然隐藏?换句话说,基于模型中的数据,我如何告诉一个元素它应该被隐藏?所以你是用户,点击一些东西或提交一些东西,然后li项显示/隐藏 然后他们离开现场,可能明天或以后再来 我将设置一个cookie,其中包含要隐藏的li项的字符串 function hideItems(string) { var partsArray = string.split('-')

我有一个“li”标签列表,我可以根据用户输入通过RJS隐藏和显示没有问题,但是数据保存了什么,然后用户第二天回来,我如何使页面中的元素仍然隐藏?换句话说,基于模型中的数据,我如何告诉一个元素它应该被隐藏?

所以你是用户,点击一些东西或提交一些东西,然后li项显示/隐藏

然后他们离开现场,可能明天或以后再来

我将设置一个cookie,其中包含要隐藏的li项的字符串

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();
    }

}
我将有一个jquery函数,我可以将该字符串传递给它。在我的站点主体中,我会有一点类似(伪代码)

如果存在cookie{
//body标签中的echo函数
echo'onload=“hideItems('.$\u COOKIE['hiddenItems'])”;
//所以如果有一个cookie,他们最终会得到这样的代码
// 
}
您需要一个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();
    }

}
函数隐藏项(字符串){
var partsArray=string.split('-');
//这将把所有列表项放入一个数组中
//例如partsArray[0]==12和partsArray[1]==23
//现在,当所有项目都在一个数组中时,您应该能够遍历并隐藏与这些值匹配的任何列表项目
//例如
对于(i=0;i<{partsArray}.length;i++){
//jquery隐藏列表项
$('#listItem'+partsArray[$i]).hide();
}
}
我可以想象,您的列表项需要采用如下格式:

<ul>
    <li id="listItem12"></li>
    <li id="listItem23"></li>
</ul>
所以我认为这就是使用javascript的方法

当然,在加载javascript并将cookie解析为ruby之前,您可以在服务器端执行此操作。如果列表项编号包含在cookie中,则向列表项回显一个隐藏类。简单

我不懂ruby,所以我编辑的cookie代码是基于php行话的


祝你好运。希望这有帮助。

那么你是用户,点击了一些东西或提交了一些东西,然后li项显示/隐藏了吗

然后他们离开现场,可能明天或以后再来

我将设置一个cookie,其中包含要隐藏的li项的字符串

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();
    }

}
我将有一个jquery函数,我可以将该字符串传递给它。在我的站点主体中,我会有一点类似(伪代码)

如果存在cookie{
//body标签中的echo函数
echo'onload=“hideItems('.$\u COOKIE['hiddenItems'])”;
//所以如果有一个cookie,他们最终会得到这样的代码
// 
}
您需要一个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();
    }

}
函数隐藏项(字符串){
var partsArray=string.split('-');
//这将把所有列表项放入一个数组中
//例如partsArray[0]==12和partsArray[1]==23
//现在,当所有项目都在一个数组中时,您应该能够遍历并隐藏与这些值匹配的任何列表项目
//例如
对于(i=0;i<{partsArray}.length;i++){
//jquery隐藏列表项
$('#listItem'+partsArray[$i]).hide();
}
}
我可以想象,您的列表项需要采用如下格式:

<ul>
    <li id="listItem12"></li>
    <li id="listItem23"></li>
</ul>
所以我认为这就是使用javascript的方法

当然,在加载javascript并将cookie解析为ruby之前,您可以在服务器端执行此操作。如果列表项编号包含在cookie中,则向列表项回显一个隐藏类。简单

我不懂ruby,所以我编辑的cookie代码是基于php行话的


祝你好运。希望这能有所帮助。

没有使用cookie,只是使用实例变量。但这基本上是正确的答案。不是用cookies做的,只是实例变量。但这基本上是正确的答案。