如何把第一列下的默认显示,其他的默认关闭。在下面的代码中修改。高手请进 10
我找了一段仿QQ菜单的js代码,具体如下:document.getElementsByClassName=function(cl){varretnode=[];varmy...
我找了一段仿QQ菜单的js代码,具体如下:
document.getElementsByClassName = function(cl) {
var retnode = [];
var myclass = new RegExp('\\b'+cl+'\\b');
var elem = this.getElementsByTagName('*');
for (var j = 0; j < elem.length; j++) {
var classes = elem[j].className;
if (myclass.test(classes)) retnode.push(elem[j]);
}
return retnode;
}
// --- 隐藏所有
function HideAll() {
var items = document.getElementsByClassName("optiton");
for (var j=0; j<items.length; j++) {
items[j].style.display = "none";
}
}
// --- 设置cookie
function setCookie(sName,sValue,expireHours) {
var cookieString = sName + "=" + escape(sValue);
//;判断是否设置过期时间
if (expireHours>0) {
var date = new Date();
date.setTime(date.getTime + expireHours * 3600 * 1000);
cookieString = cookieString + "; expire=" + date.toGMTString();
}
document.cookie = cookieString;
}
//--- 获取cookie
function getCookie(sName) {
var aCookie = document.cookie.split("; ");
for (var j=0; j < aCookie.length; j++){
var aCrumb = aCookie[j].split("=");
if (escape(sName) == aCrumb[0])
return unescape(aCrumb[1]);
}
return null;
}
window.onload = function() {
var show_item = "opt_1";
if (getCookie("show_item") != null) {
show_item= "opt_" + getCookie("show_item");
}
document.getElementById(show_item).style.display = "block";
var items = document.getElementsByClassName("title");
for (var j=0; j<items.length; j++) {
items[j].onclick = function() {
var o = document.getElementById("opt_" + this.name);
if (o.style.display != "block") {
HideAll();
o.style.display = "block";
setCookie("show_item",this.name);
}
else {
o.style.display = "none";
}
}
}
}
body部分
<ul id="menu">
<li class="item"><a href="javascript:void(0)" class="title" name="1" style="color:#FFFFFF; padding-left:10px;">11111111</a>
<ul id="opt_1" class="optiton">
<li><a href="#">aaaaa</a></li>
</ul>
</li>
<li class="item"><a href="javascript:void(0)" class="title" name="2" style="color:#FFFFFF; padding-left:10px;">22222222</a>
<ul id="opt_2" class="optiton">
<li><a href="#">aaaaa</a></li>
</ul>
</li>
<li class="item"><a href="javascript:void(0)" class="title" name="3" style="color:#FFFFFF; padding-left:10px;">3333333</a>
<ul id="opt_3" class="optiton">
<li><a href="#">aaaaa</a></li>
</ul>
</li>
</ul> 展开
document.getElementsByClassName = function(cl) {
var retnode = [];
var myclass = new RegExp('\\b'+cl+'\\b');
var elem = this.getElementsByTagName('*');
for (var j = 0; j < elem.length; j++) {
var classes = elem[j].className;
if (myclass.test(classes)) retnode.push(elem[j]);
}
return retnode;
}
// --- 隐藏所有
function HideAll() {
var items = document.getElementsByClassName("optiton");
for (var j=0; j<items.length; j++) {
items[j].style.display = "none";
}
}
// --- 设置cookie
function setCookie(sName,sValue,expireHours) {
var cookieString = sName + "=" + escape(sValue);
//;判断是否设置过期时间
if (expireHours>0) {
var date = new Date();
date.setTime(date.getTime + expireHours * 3600 * 1000);
cookieString = cookieString + "; expire=" + date.toGMTString();
}
document.cookie = cookieString;
}
//--- 获取cookie
function getCookie(sName) {
var aCookie = document.cookie.split("; ");
for (var j=0; j < aCookie.length; j++){
var aCrumb = aCookie[j].split("=");
if (escape(sName) == aCrumb[0])
return unescape(aCrumb[1]);
}
return null;
}
window.onload = function() {
var show_item = "opt_1";
if (getCookie("show_item") != null) {
show_item= "opt_" + getCookie("show_item");
}
document.getElementById(show_item).style.display = "block";
var items = document.getElementsByClassName("title");
for (var j=0; j<items.length; j++) {
items[j].onclick = function() {
var o = document.getElementById("opt_" + this.name);
if (o.style.display != "block") {
HideAll();
o.style.display = "block";
setCookie("show_item",this.name);
}
else {
o.style.display = "none";
}
}
}
}
body部分
<ul id="menu">
<li class="item"><a href="javascript:void(0)" class="title" name="1" style="color:#FFFFFF; padding-left:10px;">11111111</a>
<ul id="opt_1" class="optiton">
<li><a href="#">aaaaa</a></li>
</ul>
</li>
<li class="item"><a href="javascript:void(0)" class="title" name="2" style="color:#FFFFFF; padding-left:10px;">22222222</a>
<ul id="opt_2" class="optiton">
<li><a href="#">aaaaa</a></li>
</ul>
</li>
<li class="item"><a href="javascript:void(0)" class="title" name="3" style="color:#FFFFFF; padding-left:10px;">3333333</a>
<ul id="opt_3" class="optiton">
<li><a href="#">aaaaa</a></li>
</ul>
</li>
</ul> 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询