将cookie设置为菜单jquery

时间:2018-05-01 07:23:39

标签: javascript jquery

我有一个代码:

我有两个按钮和两个列表。这是我的代码:



$(document).on('mouseenter', '.select-user-type-js', function() {
  $('.select-user-type-js.active').add($(this)).toggleClass('active');
  if ($(this).hasClass('select-user-type_customer')) {
    $('#buyer').show();
    $('#seller').hide();
  } else {
    $('#buyer').hide();
    $('#seller').show();
  }
});

$(document).on("click", ".select-user-type-js", function() {
  if ($(this).hasClass('select-user-type_customer')) {
    $.cookie("tab", 'select-user-type_customer');
  } else {
    $.cookie("tab", 'select-user-type_performer');
  }
});

if ($.cookie("select-user-type_customer")) {
  $('#buyer')).show();
$('#seller').hide();
$('.select-user-type_performer').not($(this)).removeClass('active');
}
else {
  $('#seller')).show();
$('#buyer').hide();
$('.select-user-type_customer').not($(this)).removeClass('active');
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a href="" class="select-user-type-js select-user-type_customer active">Customer</a>
<a href="" id="seller" class="select-user-type-js select-user-type_performer">Seller</a>

<ul id="buyer">
  <li><a href="#">Menu for buyer #1</a></li>
  <li><a href="#">Menu for buyer #2</a></li>
  <li><a href="#">Menu for buyer #3</a></li>
</ul>

<ul id="seller" style="display: none;">
  <li><a href="#">Menu for seller #1</a></li>
  <li><a href="#">Menu for seller #2</a></li>
  <li><a href="#">Menu for seller #3</a></li>
</ul>
&#13;
&#13;
&#13;

如何写入cookie菜单?当用户重新加载到页面时,保存他选择的菜单,当点击特定按钮时..?我的代码不适用于cookie

1 个答案:

答案 0 :(得分:1)

以下是Cookie

示例的demo
$(document).on('mouseenter', '.select-user-type-js', function() {
  $('.select-user-type-js.active').add($(this)).toggleClass('active');
  if ($(this).hasClass('select-user-type_customer')) {
    $('#buyer').show();
    $('#seller').hide();
  } else {
    $('#buyer').hide();
    $('#seller').show();
  }
});

$(document).on("click", ".select-user-type-js", function(e) {
  e.preventDefault()
  if ($(this).hasClass('select-user-type_customer')) {
    Cookies.set("tab", 'select-user-type_customer');
  } else {
    Cookies.set("tab", 'select-user-type_performer');
  }
});

if (Cookies.get("tab") == 'select-user-type_customer') {
  $('#buyer').show();
  $('#seller').hide();
  $('.select-user-type_customer').addClass('active');
} else {
  $('#seller').show();
  $('#buyer').hide();
  $('.select-user-type_performer').AddClass('active');
}

当您需要检查Cookie值时,您必须Cookies.get("tab") == 'select-user-type_customer'而不是Cookies.get("select-user-type_customer")