根据子元素的内容对父元素排序-javascript/jquery
我有一个月历,显示当天发生的事件。生成的HTML是这样的:根据子元素的内容对父元素排序-javascript/jquery,javascript,jquery,Javascript,Jquery,我有一个月历,显示当天发生的事件。生成的HTML是这样的: <table class="event-cal"> <tbody> <tr class="eventcont event-93"> <td class="eventtime">19:00</td> <td><a class="calendar-title" href="#>Event title</a&
<table class="event-cal">
<tbody>
<tr class="eventcont event-93">
<td class="eventtime">19:00</td>
<td><a class="calendar-title" href="#>Event title</a><br></td></tr>
<tr class="eventcont event-237">
<td class="eventtime">13:00</td>
<td><a class="calendar-title" href="#">Event 2 title</a><br></td></tr>
</tbody>
</table>
您需要使用为每个表运行函数
您需要使用为每个表运行函数 选中此项
$(".event-cal").each(function(){
var events=$('tr',this);
events.sort(function (a, b) {
a = parseInt($('.eventtime',a).text().replace(':',''));
b = parseInt($('.eventtime',b).text().replace(':',''));
if(a > b) {
return 1;
} else if(a < b) {
return -1;
} else {
return 0;
}
});
$(this).html(events);
});
$(“.event cal”)。每个(函数(){
var事件=$('tr',this);
事件排序(函数(a,b){
a=parseInt($('.eventtime',a).text().replace(':','');
b=parseInt($('.eventtime',b).text().replace(':','');
如果(a>b){
返回1;
}否则如果(a
检查此项
$(".event-cal").each(function(){
var events=$('tr',this);
events.sort(function (a, b) {
a = parseInt($('.eventtime',a).text().replace(':',''));
b = parseInt($('.eventtime',b).text().replace(':',''));
if(a > b) {
return 1;
} else if(a < b) {
return -1;
} else {
return 0;
}
});
$(this).html(events);
});
$(“.event cal”)。每个(函数(){
var事件=$('tr',this);
事件排序(函数(a,b){
a=parseInt($('.eventtime',a).text().replace(':','');
b=parseInt($('.eventtime',b).text().replace(':','');
如果(a>b){
返回1;
}否则如果(a
您将附加到$(“.event-cal”),这是每个具有类“event-cal”的表。如果您希望它只将其附加到正在查找它们的表中,则需要为每个表appart指定此函数,如答案中所示。应该注意的是,排序函数似乎有缺陷,例如“10:00”<“2:00”
等。您将附加到$(“.event cal”),这是每个具有类“event cal”的表。如果您希望它只将其附加到您正在查找它们的表中,则需要为每个表appart指定此函数,如答案中所示。应该注意的是,排序函数似乎有缺陷,例如“10:00”<“2:00”
等@Yury,您能解释一下此函数吗?我正在测试它,但我知道它不是defined@VixedsortDesc是Daria在问题上创建的函数。对不起,ups!我一直在研究我的答案,这就是为什么我没有看到它,哈哈,我没有用;)@尤里,你能解释一下这个功能吗?我正在测试它,但我知道它不是defined@VixedsortDesc是Daria在问题上创建的函数。对不起,ups!我一直在研究我的答案,这就是为什么我没有看到它,哈哈,我没有用;)
$(".event-cal").each(function(){
var events=$('tr',this);
events.sort(function (a, b) {
a = parseInt($('.eventtime',a).text().replace(':',''));
b = parseInt($('.eventtime',b).text().replace(':',''));
if(a > b) {
return 1;
} else if(a < b) {
return -1;
} else {
return 0;
}
});
$(this).html(events);
});