【问题标题】:function showHide(shID) with jquery-cookie带有 jquery-cookie 的函数 showHide(shID)
【发布时间】:2015-02-06 16:17:32
【问题描述】:

我需要将 jquery-cookie 用于函数 showHide(shID) 有人知道该怎么做吗?我有一个 onclick 按钮来显示更多内容,使用函数 showHide(shID) 但我只需要隐藏一次。那么可以为它添加 jquery-cookie 吗?我该怎么做? [https://github.com/carhartl/jquery-cookie#readme][1]

这里是我的代码:function showHide(shID)

<script language="javascript" type="text/javascript">
function showHide(shID) {
   if (document.getElementById(shID)) {
      if (document.getElementById(shID+'-show').style.display != 'none') {
         document.getElementById(shID+'-show').style.display = 'none';
         document.getElementById(shID).style.display = 'block';
      }
      else {
         document.getElementById(shID+'-show').style.display = 'inline';
         document.getElementById(shID).style.display = 'none';
      }
   }
}
</script>

拜托,我需要有人指导我或让我知道该怎么做。提前致谢!

【问题讨论】:

    标签: javascript jquery cookies show-hide jquery-cookie


    【解决方案1】:

    是的,可以使用 cookie 来使用 cookie,您必须包含 cookie 文件和 jquery 文件。我举个例子。

    if($.cookie("example")=='1') {
        //get cookie and your hide code here.
    } else {
        //set cookie
        $.cookie("example", "1");
    }
    

    【讨论】:

    • 嗨桑尼!很高兴再次见到你!再次感谢您的帮助.. 代码是这样写的吗?抱歉,我是 javascript jsfiddle.net/Garfrey/5xf72j4m 的新手
    • 你能把你的完整代码放上来,我可以更好地帮助你。
    • 检查一下我已经完成了 cookie 代码的工作。 jsfiddle.net/sunnyept/xpv8nood
    • Sunny,这些cookie代码可以在显示内容上起作用吗?意味着当人们点击“阅读更多”时,隐藏的内容就会显示出来,并且cookie会影响隐藏功能只会使用一次。 jsfiddle.net/Garfrey/5xf72j4m/8
    • 是的,它会起作用,但您必须根据您的要求编写代码。我刚刚完成了设置 cookie 和获取 cookie 的代码。
    【解决方案2】:

    这是一个答案,由 Ionica Bizau https://stackoverflow.com/users/1420197/ionic%C4%83-biz%C4%83u解决

    function showHide(setCookie) {
        var shID = $(this).data("showhide")
          , $shEl = $("#" + shID)
          , $showHide = $("#" + shID + '-show')
          ;
    
        if ($shEl.is(":hidden")) {
            if (setCookie !== false) {
                jQuery.cookie("showhide-" + shID, 1);
            }
            $showHide.hide();
            $shEl.show();
        } else {
            if (setCookie !== false) {
                jQuery.cookie("showhide-" + shID, 0);
            }
            $showHide.show();
            $shEl.hide();
        }
    }
    
    jQuery(document).ready(function () {
        $("#example-show").on("click", showHide);
        if (jQuery.cookie("showhide-" + "example") == '1') {
            showHide.call($("#example-show").get(0), false);
        }
    });
    

    JQuery Cookie not function

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多