<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title><script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<style type="text/css">
.button{ width:150px; height:30px; line-height:30px;}
.butselect{width:150px; height:30px; line-height:30px; background-color:#FFF;}
.div{ width:500px; height:100px; background-color:Red; display:none;}
.divselect{ width:500px; height:100px; background-color:Red;}
.coloseselect{width:15px; height:30px; line-height:30px; background-color:#FFF;}
.colose{width:15px; height:30px; line-height:30px; }
.buttonh{ display:none;}
</style>
</head>
<body>
<button id="addnewtabs">增加新tabs</button>
<div id="tabstest">
<button id="tabsm1" class="butselect" name="tabs">tabs1</button><button name="colose" id="tabsc1" class="coloseselect">x</button><button id="tabsm2" name="tabs" class="button">tabs2</button><button name="colose" id="tabsc2" class="colose">x</button><button id="tabsm3" name="tabs" class="button">tabs3</button><button name="colose" id="tabsc3" class="colose">x</button>
</div><div id="tabsbody">
<div id="tabsb1" class="divselect" name="body" >tabsb1</div>
<div id="tabsb2" class="div" name="body">tabsb2</div>
<div id="tabsb3" class="div" name="body">tabsb3</div></div>
</body><script type="text/javascript">
var x = 0; //定义一个全局变量 用来存储tabs的当前的最大索引号
$("#addnewtabs").bind("click", function () {
$("button[name='colose']").each(function () {
var count = $(this).attr("id");
count = count.substr(count.length - 1, 1);
count = count - 1 + 1;
if (count > x) {
x = count;
}
})
x = x + 1;
var tabsm = "<button id=\"tabsm" + x + "\" class=\"butselect\" name=\"tabs\">tabs" + x + "</button>";
var tabsc = "<button name=\"colose\" id=\"tabsc" + x + "\" class=\"coloseselect\">x</button>";
var tabsb = "<div id=\"tabsb" + x + "\" class=\"divselect\" name=\"body\" >tabsb" + x + "</div>";
var oldm = $(".butselect").attr("id");
var oldc = $(".divselect").attr("id");
var oldb = $(".coloseselect").attr("id");
$("#" + oldm).removeClass();
$("#" + oldm).addClass("button");
$("#" + oldc).removeClass();
$("#" + oldc).addClass("div");
$("#" + oldb).removeClass();
$("#" + oldb).addClass("colose");
$("#tabsbody").append(tabsb);
$("#tabstest").append(tabsm);
$("#tabstest").append(tabsc);
})
// $("#tabsm1").addClass("butselect");
$("button[name='tabs']").live("click", function () {
// $("button").removeClass();
// $(this).addClass("butselect");
var x = $(this).attr("id");
x = x.replace(/tabsm/, "");
x = x - 1 + 1;
// $(".div").addClass("div")
// $(".div").children("#tabsb" + x).removeClass()
// $("#tabsb" + x).addClass("divsetect")
// alert(x);
// var count = 0;
// $("button[name='tabs']:visible").removeClass();
// $("div[name='body']:visible").removeClass();
// $("button[name='colose']:visible").removeClass();
$("button[name='colose']:visible").each(function () {
var count = $(this).attr("id");
count = count.replace(/tabsc/, "");
count = count - 1 + 1;
if (count == x) {
$("#tabsb" + count).removeClass();
$("#tabsm" + count).removeClass();
$("#tabsc" + count).removeClass();
$("#tabsb" + count).addClass("divselect");
$("#tabsm" + count).addClass("butselect");
$("#tabsc" + count).addClass("coloseselect");
}
else {
$("#tabsb" + count).removeClass();
$("#tabsm" + count).removeClass();
$("#tabsc" + count).removeClass();
$("#tabsb" + count).addClass("div");
$("#tabsm" + count).addClass("button");
$("#tabsc" + count).addClass("colose");
}
});
})
$("button[name='colose']").live("click", function () {
var x = $(this).attr("id");
x = x.replace(/tabsc/, "");
x = x - 1 + 1;
$("#tabsm" + x).remove();
$("#tabsc" + x).remove();
$("#tabsb" + x).remove();
var y = 0;
$("button[name='colose']:visible").each(function () {
var count = $(this).attr("id");
count = count.replace(/tabsc/, "");
count = count - 1 + 1;
if (count != x && y == 0) {
$("#tabsb" + count).removeClass();
$("#tabsm" + count).removeClass();
$("#tabsc" + count).removeClass();
$("#tabsb" + count).addClass("divselect");
$("#tabsm" + count).addClass("butselect");
$("#tabsc" + count).addClass("coloseselect");
y++;
}
else {
$("#tabsb" + count).removeClass();
$("#tabsm" + count).removeClass();
$("#tabsc" + count).removeClass();
$("#tabsb" + count).addClass("div");
$("#tabsm" + count).addClass("button");
$("#tabsc" + count).addClass("colose");
}
})
})
</script>
</html>