目的:做一个文章列表,ajax添加了内容的基本机构是这样的<ul>年<li>月<ul><li>日</li></ul></li></ul>,要实现的功能是点击年出现月的列表,点击月出现日的列表,因为内容都是ajax添加,我用live绑定了年和月的click事件。问题如下:点击年出现了月列表,这是正常的,但点击月的时候,同时激发了父级年的click事件,日的列表一闪就没了,又变成了点击年后出现月的样子。这个问题有没有好的解决办法?$(function() {
var date_type = "";
get_li();
function get_li() {
$.ajax({
type : "post",
dataType : "html",
url : "member/content_li.php",
data : {
q_id : $("#qid").html(),
date_type : date_type
},
success : function(res_year) {
$("#date_li").html(res_year);
var year = ""
$(".year").live("click", function() {
year = $(this).html().substr(0,4);
$.ajax({
type : "post",
dataType : "html",
url : "member/content_li.php",
data : {
q_id : $("#qid").html(),
date_type : "year",
year : year
},
success : function(res_month) {
$("li[year=" + year + "]").html(res_month);
var month = ""
$(".month").live("click", function() {
month = $(this).html().substr(0,2);
$.ajax({
type : "post",
dataType : "html",
url : "member/content_li.php",
data : {
q_id : $("#qid").html(),
date_type : "month",
year : year,
month : month
},
success : function(res_day) {
$("ul[month=" + month + "]").html(res_day);
$(".day").live("click", function() {
day = $(this).html().substr(0,2);
var date = year + "-" + month + "-" + day;
$.ajax({
type : "post",
dataType : "json",
url : "member/content_li.php",
data : {
q_id : $("#qid").html(),
date_type : "day",
date : date
},
success : back_do11
})
})
}
})
})
}
})
})
}
})
}
var date_type = "";
get_li();
function get_li() {
$.ajax({
type : "post",
dataType : "html",
url : "member/content_li.php",
data : {
q_id : $("#qid").html(),
date_type : date_type
},
success : function(res_year) {
$("#date_li").html(res_year);
var year = ""
$(".year").live("click", function() {
year = $(this).html().substr(0,4);
$.ajax({
type : "post",
dataType : "html",
url : "member/content_li.php",
data : {
q_id : $("#qid").html(),
date_type : "year",
year : year
},
success : function(res_month) {
$("li[year=" + year + "]").html(res_month);
var month = ""
$(".month").live("click", function() {
month = $(this).html().substr(0,2);
$.ajax({
type : "post",
dataType : "html",
url : "member/content_li.php",
data : {
q_id : $("#qid").html(),
date_type : "month",
year : year,
month : month
},
success : function(res_day) {
$("ul[month=" + month + "]").html(res_day);
$(".day").live("click", function() {
day = $(this).html().substr(0,2);
var date = year + "-" + month + "-" + day;
$.ajax({
type : "post",
dataType : "json",
url : "member/content_li.php",
data : {
q_id : $("#qid").html(),
date_type : "day",
date : date
},
success : back_do11
})
})
}
})
})
}
})
})
}
})
}
e.stopPropagation();//阻止冒泡
})
其他类似一样加上;
var date_type = "";
get_li();
var year = "";
var month = "";
function get_li() {
$.ajax({
type : "post",
dataType : "html",
url : "member/content_li.php",
data : {
q_id : $("#qid").html(),
date_type : date_type
},
success : function(res_year) {
$("#date_li").html(res_year);
}
})
}
$(".year").live("click", function(e) {
var n=$(this).children().length;
if(n>0){
if($(this).children().is(":hidden")){
$(this).children().show();
}else{
$(this).children().hide();
}
}else{
year = $(this).html().substr(0,4);
$.ajax({
type : "post",
dataType : "html",
url : "member/content_li.php",
data : {
q_id : $("#qid").html(),
date_type : "year",
year : year
},
success : function(res_month) {
$("ul[year=" + year + "]").append(res_month);
}
})
}
e.stopPropagation();//阻止冒泡
})
$(".month").live("click", function(e) {
var n=$(this).children().length;
if(n>0){
if($(this).children().is(":hidden")){
$(this).children().show();
}else{
$(this).children().hide();
}
}else{
month = $(this).html().substr(0,2);
$.ajax({
type : "post",
dataType : "html",
url : "member/content_li.php",
data : {
q_id : $("#qid").html(),
date_type : "month",
year : year,
month : month
},
success : function(res_day) {
$("ul[month=" + month + "]").append(res_day);
}
})
}
e.stopPropagation();//阻止冒泡
})
$(".day").live("click", function(e) {
day = $(this).html().substr(0,2);
var date = year + "-" + month + "-" + day;
$.ajax({
type : "post",
dataType : "json",
url : "member/content_li.php",
data : {
q_id : $("#qid").html(),
date_type : "day",
date : date
},
success : back_do11
})
e.stopPropagation();//阻止冒泡
})
$(".year").live(...);
$(".month").live(...);
$(".day").live(...);