澳门皇冠金沙网站-澳门皇冠844网站

热门关键词: 澳门皇冠金沙网站,澳门皇冠844网站

通过cookie实现刷新不变化树形菜单,利用原生J

经过安装cookie来保存树形菜单的事态,在页面加载时再次读取cookie来设置菜单。

贯彻原理很简短,正是循环小说模块,并抽出在那之中的h2、h3标签,将在那之中的内容予以给新建的title树。

承袭作者的上一篇,设置好cookie后,将他们抽取来

菜单的HTML结构:

代码如下:

复制代码 代码如下:

<div class="treemenu">
<ul>
<li>
<a href="#" id="treemenu_a_1">一级菜单一</a>
<div class="submenu" id="submenu_1">
<ul>
<li><a href="subpage/a.html" id="submenu_a_1_1">二级菜单一</a></li>
<li><a href="subpage/b.html" id="submenu_a_1_2">二级菜单二</a></li>
<li><a href="#" id="submenu_a_1_3">二级菜单三</a></li>
<li><a href="#" id="submenu_a_1_4">二级菜单四</a></li>
<li><a href="#" id="submenu_a_1_5">二级菜单五</a></li>
</ul>
</div>
</li>
<li>
<a href="#" id="treemenu_a_2">一级菜单二</a>
<div class="submenu" id="submenu_2">
<ul>
<li>
<a href="#" id="submenu_a_2_1">二级菜单一</a>
<div class="submenu" id="submenu_2_1">
<ul>
<li><a href="#" id="submenu_a_2_1_1">三级菜单一</a></li>
<li><a href="#" id="submenu_a_2_1_2">三级菜单二</a></li>
<li>
<a href="#" id="submenu_a_2_1_3">三级菜单三</a>
<div class="submenu" id="submenu_2_1_3">
<ul>
<li><a href="#" id="submenu_a_2_1_3_1">四级菜单一</a></li>
<li><a href="#" id="submenu_a_2_1_3_2">四级菜单二</a></li>
<li><a href="#" id="submenu_a_2_1_3_3">四级菜单三</a></li>
</ul>
</div>
</li>
</ul>
</div>
</li>
<li><a href="#" id="submenu_a_2_2">二级菜单二</a></li>
<li><a href="#" id="submenu_a_2_3">二级菜单三</a></li>
<li><a href="#" id="submenu_a_2_4">二级菜单四</a></li>
<li><a href="#" id="submenu_a_2_5">二级菜单五</a></li>
</ul>
</div>
</li>
<li>
<a href="#" id="treemenu_a_3">一级菜单三</a>
<div class="submenu" id="submenu_3">
<ul>
<li><a href="#" id="submenu_a_3_1">二级菜单一</a></li>
<li><a href="#" id="submenu_a_3_2">二级菜单二</a></li>
<li><a href="#" id="submenu_a_3_3">二级菜单三</a></li>
<li><a href="#" id="submenu_a_3_4">二级菜单四</a></li>
<li><a href="#" id="submenu_a_3_5">二级菜单五</a></li>
</ul>
</div>
</li>
</ul>
</div>

HTML代码:

var userName = Ext.util.Cookies.get('userName');
var userAuthority = Ext.util.Cookies.get('userAuthority');//0,1,2

读取cookie工具类:

<div class="contextBox">
 <div id="article">
  <h2>二级标题</h2>
  <h3>三级标题</h3>
  <p>hello hello hello hello hello hello hello hello hello hello hello hello</p>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <p>hello hello hello hello hello hello hello hello hello hello hello</p>
  <h2>二级标题</h2>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <p>world world world world world world world world world world world world world world world world world world world world world world world world world world world world world world world </p>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <h2>二级标题</h2>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <h2>二级标题</h2>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
  <h2>二级标题</h2>
  <h3>三级标题</h3>
  <h3>三级标题</h3>
 </div>
 <div class="articleMenu-box" id="articleMenu_box">

  <ul class="articleMenu hello" id="articleMenu">

  </ul>
 </div>
</div>

接下去就能够用了

//cookie工具类
var cookieTool = {
//读取cookie
getCookie: function(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name   "=");
if (c_start != -1) {
c_start = c_start   c_name.length   1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) {
c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
},
//设置cookie
setCookie: function(c_name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate()   expiredays); //设置日期
document.cookie = c_name   "="   escape(value)   ((expiredays == null) ? "" : ";expires="   exdate.toGMTString());
},
//删除cookie
delCookie: function(c_name) {
var exdate = new Date();
exdate.setDate(exdate.getDate() - 1); //昨天日期
document.cookie = c_name   "=;expires="   exdate.toGMTString();
}
};

CSS代码:

复制代码 代码如下:

美食做法事件绑定:

* {
 margin: 0;
 padding: 0;
 border: 0;
}
body {
 font: 16px/1.5;
}
ul li, ol li {
 list-style: none;
}
.contextBox {
 position: relative;
 width: 960px;
 margin: 0 auto;
}
#article {
 margin-left: 200px;
 border: 1px #eee solid;
 padding: 15px;
}
.articleMenu a {
 text-decoration: none;
 color: #333;
}
.articleMenu a:hover {
 color: #f85455;
}
.articleMenu-box {
 width: 170px;
 position: absolute;
 left: 10px;
 top: 10px;
}
.articleMenu {
 padding: 10px;
 border: 1px solid #ccc;
 box-shadow: 2px 2px 2px #eee;
}
.titleH2, .titleH3 {
 line-height: 1.5em;
}
.titleH2 {
 font-weight: bold;
}
.titleH3 {
 margin-left: 20px;
}
.articleMenu .articleMenu-close, .articleMenu-open {
 display: inline-block;
 position: absolute;
 right: 0;
 top: 0;
 height: 44px;
 width: 44px;
 cursor: pointer;
}
.articleMenu-open {
 backgroundnull:url("http://www.dengzhr.com/wp-content/themes/dengzhr/images/icon_articleMenu_open.png") no-repeat 50% 50%;
 display: none;
}
.articleMenu .articleMenu-close {
 backgroundnull:url("http://www.dengzhr.com/wp-content/themes/dengzhr/images/icon_articleMenu_close.png") no-repeat 50% 50%;
}

//树形菜单依据分歧权限展现
var treeUrl='../store/ccTree' userAuthority '.json';

//菜单事件绑定
$('.treemenu a').bind('click', function() {
var $this = $(this);
var id = $this.attr('id');
var $submenu = $this.next('.submenu');
if ($submenu.length > 0) { //是否有子菜单
var flag = $(this).next('.submenu:hidden').length > 0 ? true : false;
if (flag) {
$submenu.show();
} else {
$submenu.hide();
}
var display = flag ? 'block' : 'none';
cookieTool.setCookie(id, display, 10);
} else {
cookieTool.setCookie(id, id, 10);
var curId = cookieTool.getCookie(id);
$('.treemenu').find('.on').removeClass('on').addClass('off');
$('#'   curId).addClass('on');
$('.treemenu a[class="off"]').each(function() { 
cookieTool.delCookie($(this).attr('id')); //删除其他已选择选项cookie
});
}
});

JavaScript代码:

var treeStore = Ext.create('Ext.data.TreeStore', {

页面加载时再次设置菜单

var article = document.getElementById("article");
var articleHgroupMenu = document.getElementById("articleMenu");

// 关闭和展开文档树
var articleMenu_open = document.getElementById("articleMenu_open");
var articleMenu_close = document.getElementById("articleMenu_close");
articleMenu_close.onclick = function() {
 articleHgroupMenu.style.display = "none";
 articleMenu_open.style.display = "block";
};
articleMenu_open.onclick = function() {
 articleHgroupMenu.style.display = "block";
 articleMenu_open.style.display = "none";
};

//
titleHgroup(article, articleHgroupMenu, "titleH2", "titleH3");

// 获得obj下的直接子元素中为标题h2~h3的标题元素
// 参数说明:hgroupParent为包含h2和h3的直接父元素;MenuList为承载新建文章列表的ul元素;
// h2ClassName、h3ClassName分别为新建文章列表中对应h2、h3的li自列表的Class属性;
function titleHgroup(hgroupParent, MenuList, h2ClassName, h3ClassName) {
 var hgroup = hgroupParent.children;

 // 创建文档片段,来包裹自动生成的h2、h3对应生成的li列表
 var fragment = document.createDocumentFragment();
 for(i = 0; i < hgroup.length && hgroup[i].nodeType === 1; i  ) {

  // 为对应类型的标题生成li列表
  // 参数说明:hType为标题的类型如h1~h6;className为标题对应的li列表的class属性值;
  function titleToList(hType, className) {
   var li = document.createElement("li");
   li.className = className;

   // 为li标签内部添加a标签,用锚点进行定位;
   hgroup[i].id= hType   i;
   li.innerHTML = ("<a href='#"   hType   i   "'>"   hgroup[i].innerHTML  "</a>");
   fragment.appendChild(li);
  }

  // 当遍历中标题元素为h2时,调用titleToList(hType, className)新增对应的li列表;
  if(hgroup[i].nodeName.toLowerCase() == "h2") {
   titleToList("h2", h2ClassName);
  }

  // 当遍历中标题元素为h3时,调用titleToList(hType, className)新增对应的li列表;
  if(hgroup[i].nodeName.toLowerCase() == "h3") {
   titleToList("h3", h3ClassName);
  }
 }
 // 将承载好对应li元素集合的文档片段fragment添加到DOM(即在DOM中包裹li列表的父元素)中去;
 MenuList.appendChild(fragment);
}

proxy:{
type:'ajax',
url:treeUrl
}
});

本文由澳门皇冠金沙网站发布于前端开发,转载请注明出处:通过cookie实现刷新不变化树形菜单,利用原生J