English 中文(简体)
采用舱面法从超文本中获取的时间数据
原标题:Parse time data from HTML using jQuery

我在我的一页上翻了一番 j,为逊尼派和逊尼派产出。 下面的法典是产出(假设这些时间每天都发生变化)。 我在植被产出上没有控制。

<div id="sun_container">
    <div class="details">
        Sunrise: <strong>7:00AM</strong> |
        Sunset: <strong>4:30PM</strong>
    </div>
</div>

在我网页上的其他地方,我有一个形象标签,看上去:

<div id="sun_button">
    <img src="images/day.png">
</div>

I want to parse out the sunrise time and the sunset time and compare these against the current server time (which I can output via PHP if necessary).

如果目前的服务器时间不是,在太阳度和太阳度之间,那么,我想改变形象src>>mages/week.png”

关于我如何能够这样做的任何想法?

http://www.un.org。 如今,Im在网页<head>上输出服务器时间,使用:

var服务器_time = “<?=date(”H:i:s”, time()?>”

产出如下:

<代码>var服务器_time = “17:07:41”;

最佳回答

You can do it very easily with the DateJS JavaScript library:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="http://www.datejs.com/build/date.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 var sunrise = Date.parse($(".details strong:eq(0)").text());
 var sunset = Date.parse($(".details strong:eq(1)").text());
    if(Date.parse("" + server_time).between(sunrise, sunset)) { 
       $("img").attr("src","images/day.png")
    }
    else {
       $("img").attr("src","images/night.png")
    }
});
</script>

http://jsfiddle.net/tsabd/"rel=“nofollow”> 在这里打听!

问题回答

你们可以通过做如下事情来获得太阳/太阳时代:

var sunrise = $( #sun_container .details strong:eq(0) ).text();
var sunset = $( #sun_container .details strong:eq(1) ).text();

从那里可以把服务器时间写到联合材料,进行对比,然后:

if (isNight) {
    $( #sun_button img ).attr( src ,  images/night.png );
}

That should get you started.

假设你可以产生24小时的集装箱:

<div id="server_time">18:00</div>
<script>
$(function() {
// Assume server_time is 24hr
var server = new Date(Date.parse("2000-01-01 " + $( #server_time ).text()));
var sunrise = $( #sun_container .details strong:eq(0) ).first().text();
var sunset = $( #sun_container .details strong:eq(1) ).last().text();

// strip off AM/PM
sunrise = sunrise.substring(0, sunrise.length-2);
sunset = sunset.substring(0, sunset.length-2);

// Parse to standard dates
sunrise = new Date(Date.parse("2000-01-01 " + sunrise)) ;
sunset = new Date(Date.parse("2000-01-01 " + sunset));
// Add tweleve hours to compensate for PM
sunset.setHours(sunset.getHours() + 12);

if (server < sunrise || server > sunset)
    $( #sun_button > img ).attr( src ,  images/night );
});
</script>




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签