我想获得jquery的帮助。我在HTML元素中遇到了返回值的问题。我正在研究时间1和时间2之间的时间,例如:下午4:30带走的时间是下午6:30,返回值为2.00。
以下是HTML的示例:
<span id='time1'>4:30 PM</span> - <span id='title1'>The Scorpion King</span><br></br>
<span id='time2'>6:30 PM</span> - <span id='title2'>Gone in Sixty Seconds</span><br></br>
<span id='time3'>9:00 PM</span> - <span id='title3'>The Fast and the Furious</span><br></br>
<span id='time4'>11:30 PM</span> - <span id='title4'>The Fast and the Furious</span><br></br>
<span id='time5'>2:00 AM</span> - <span id='title5'>The Scorpion King</span><br></br>
<span id='time6'>4:00 AM</span> - <span id='title6'>New Sheer Cover Studio mineral makeup</span><br></br>
<span id='time7'>8:49 PM</span> - <span id='title7'>Tamra's O.C. Wedding</span><br></br>
在这种情况下,我有一个返回值,它将从time1和time2显示'-10.00',当我计算出返回值应该显示为'2.00'2小时。还有其他一些值相同,它会让我迷惑时间会持续几个小时。我没有任何其他返回值的问题,当我在不同的时间锻炼时,我会得到类似1.00,1.30,2.00,2.30 ......的内容。我想在没有得到返回值'-10.00',' - 11.00'或者使用什么东西的情况下找出它能持续多久的时间,就像'1.00','2.00',在做某事之前'3.00'......等。
我已经尝试过这样的代码,但它会得到错误的返回值,比如'-10.00',' - 11.00'或者它应该是1.00,2.00或者其他任何东西。
function getSchedule($link,j)
{
var widthval = 850;
var parts = $link.split("/");
var links = parts[parts.length-1];
var programlength = 0;
$.ajax({
url:$.trim(links),
type:'GET',
data:'',
success: function(data)
{
var $data = $(data);
var title1 = $data.filter("#title1").html();
var title2 = $data.filter("#title2").html();
var time1 = $data.filter("#time1").html();
var time2 = $data.filter("#time2").html();
time1 = time1.split(" ");
var time1AMPM = time1[1];
time1 = time1[0].split(":");
time1= time1[0]+'.'+time1[1];
if($.trim(time1AMPM) == 'AM' && time1<12)
time1 = parseFloat(time1)+12;
else if($.trim(time1AMPM) == 'PM' && time1<12)
time1 = parseFloat(time1)+12;
time2 = time2.split(" ");
var time2AMPM = time2[1];
time2 = time2[0].split(":");
time2= time2[0]+'.'+time2[1];
if($.trim(time2AMPM) == 'AM' && time2<12)
time2 = parseFloat(time2)+12;
else if($.trim(time2AMPM) == 'PM' && time2<12)
time2 = parseFloat(time2)+12;
var difftime2time1 = (parseFloat(time2) - parseFloat(time1)).toFixed(2);
var difftime3time2 = (parseFloat(time3) - parseFloat(time2)).toFixed(2);
if(isNaN(difftime2time1))
{
difftime2time1=0;
}
if(isNaN(difftime3time2))
{
difftime3time2=0;
}
var currenttotal = 0;
var firstele = ((j-1)*1)+1; // how many programme i want to output in per block
var lastele = parseInt(firstele)+1; // how many programme i want to output AFTER the firstele
var k = 1;
var programlength = 0;
for(;firstele <= lastele;firstele++)
{
var nexttimedate = parseInt(k)+1;
var programlength = eval('difftime'+nexttimedate+'time'+k);
//alert(programlength);
if(programlength >= 1.00 && programlength <= 1.29)
{
$('#programe'+firstele).addClass("span1hr");
width[j]=517;
}
if(programlength >= 1.30 && programlength <= 1.80)
{
$('#programe'+firstele).addClass("span1_5hr");
width[j]=784;
}
if(programlength >= 1.81 && programlength <= 1.99)
{
$('#programe'+firstele).addClass("span2hr");
width[j]=784;
}
if(programlength >= 2.00 && programlength <= 2.29)
{
$('#programe'+firstele).addClass("span2hr");
width[j]=1051;
}
if(programlength >= 2.30 && programlength <= 2.80)
{
$('#programe'+firstele).addClass("span2_5hr");
width[j]=1318;
}
//sort this out--------1 hour
if(programlength >= 13.00 && programlength <= 13.29)
{
$('#programe'+firstele).addClass("span1hr");
width[j]=517;
}
//sort this out--------1 hour and 30 mins
if(programlength >= 13.30 && programlength <= 13.80)
{
$('#programe'+firstele).addClass("span1_5hr");
width[j]=517;
}
//sort this out--------2 hour
if(programlength >= 14.00 && programlength <= 14.29)
{
$('#programe'+firstele).addClass("span2hr");
width[j]=1585;
}
//sort this out-------- 2 hours and 30 mins
if(programlength >= 14.30 && programlength <= 14.80)
{
$('#programe'+firstele).addClass("span2_5hr");
width[j]=1585;
}
if(programlength == -10.00) //sort this out ---- 2 hour program
{
$('#programe'+firstele).addClass("span2hr");
width[j]=1051;
}
currenttotal++;
pgmcontent[firstele] = eval('title'+k);
k++;
}
checksum+=j;
if(checksuminit == checksum)
{
for(var ii=1;ii<width.length-1;ii++)
{
widthval+=width[ii];
}
for(var jj=1;jj <= pgmcontent.length-1;jj++)
{
$('#programe'+jj).html(pgmcontent[jj]);
}
for(var kk=1;kk <= imagecontent.length-1;kk++)
{
$('#image'+kk).html(imagecontent[kk]);
}
$("body").find('.rowSubPgm').each(function(index) {
$(this).css( "width", widthval+"px");
});
}
}
});
}
我不能将jsfiddle发布为示例,因为当我计算出时间时警报不会显示。但是我很高兴发布我的网站链接,因此您可以看到差异值为'1.00,2.00,'13 .00','14 .00'和'-10.00'的提醒消息。你会知道我到底在说什么。
我的网站链接:
答案 0 :(得分:1)
如果你有一个时间从上午11点开始结束为下午1点,你的代码目前正在增加12小时AM和PM,导致13 - 23 = -10
时间是AM,目前是:
if($.trim(time1AMPM) == 'AM' && time1<12)
time1 = parseFloat(time1)+12;
尝试:
if($.trim(time1AMPM) == 'AM')
time1 = parseFloat(time1);
时间相同。
然后,您还需要添加支票,以查看午夜过夜的节目。例如开始于2300结束于0100.最简单的事情是:
if (difftime2time1 < 0) difftime2time1 += 24; // add on 24 hours
答案 1 :(得分:0)
感谢您的快速回复,大多数问题都已修复,但有些值显示为'-9.00',' - 8.30',' - 11.00'。我不知道如何摆脱它并使用像1.00,2.00或其他任何东西。
以下是当前代码:
time1 = time1.split(" ");
var time1AMPM = time1[1];
time1 = time1[0].split(":");
time1= time1[0]+'.'+time1[1];
if($.trim(time1AMPM) == 'AM')
time1 = parseFloat(time1);
else if($.trim(time1AMPM) == 'PM')
time1 = parseFloat(time1);
time2 = time2.split(" ");
var time2AMPM = time2[1];
time2 = time2[0].split(":");
time2= time2[0]+'.'+time2[1];
if($.trim(time2AMPM) == 'AM')
time2 = parseFloat(time2);
else if($.trim(time2AMPM) == 'PM')
time2 = parseFloat(time2);
time3 = time3.split(" ");
var time3AMPM = time3[1];
time3 = time3[0].split(":");
time3 = time3[0]+'.'+time3[1];
if($.trim(time3AMPM) == 'AM')
time3 = parseFloat(time3);
else if($.trim(time3AMPM) == 'PM')
time3 = parseFloat(time3);
time4 = time4.split(" ");
var time4AMPM = time4[1];
time4 = time4[0].split(":");
time4= time4[0]+'.'+time4[1];
if($.trim(time4AMPM) == 'AM')
time4 = parseFloat(time4);
else if($.trim(time4AMPM) == 'PM')
time4 = parseFloat(time4);
time5 = time5.split(" ");
var time5AMPM = time5[1];
time5 = time5[0].split(":");
time5= time5[0]+'.'+time5[1];
if($.trim(time5AMPM) == 'AM')
time5 = parseFloat(time5);
else if($.trim(time5AMPM) == 'PM')
time5 = parseFloat(time5);
time6 = time6.split(" ");
var time6AMPM = time6[1];
time6 = time6[0].split(":");
time6= time6[0]+'.'+time6[1];
if($.trim(time6AMPM) == 'AM')
time6 = parseFloat(time6);
else if($.trim(time6AMPM) == 'PM')
time6 = parseFloat(time6);
var difftime2time1 = (parseFloat(time2) - parseFloat(time1)).toFixed(2);
var difftime3time2 = (parseFloat(time3) - parseFloat(time2)).toFixed(2);
var difftime4time3 = (parseFloat(time4) - parseFloat(time3)).toFixed(2);
var difftime5time4 = (parseFloat(time5) - parseFloat(time4)).toFixed(2);
var difftime6time5 = (parseFloat(time6) - parseFloat(time5)).toFixed(2);
if(isNaN(difftime2time1))
{
difftime2time1=0;
}
if(isNaN(difftime3time2))
{
difftime3time2=0;
}
if(isNaN(difftime4time3))
{
difftime4time3=0;
}
if(isNaN(difftime5time4))
{
difftime5time4=0;
}
if(isNaN(difftime6time5))
{
difftime6time5=0;
}
var currenttotal = 0;
var firstele = ((j-1)*5)+1; // how many programme i want to output in per block
var lastele = parseInt(firstele)+4; // how many programme i want to output AFTER the firstele
var k = 1;
var programlength = 0;
你知道为什么一些返回值仍然显示'-9.00',' - 8.30',' - 11.00'以及我如何使返回值显示为'3.00','3.30'和'1.00'而不是'-9.00',' - 8.30',' - 11.00'?