人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當(dāng)前位置:蘿卜系統(tǒng)下載站 > 技術(shù)開(kāi)發(fā)教程 > 詳細(xì)頁(yè)面

在JavaScript中應(yīng)用Object (3)

在JavaScript中應(yīng)用Object (3)

更新時(shí)間:2021-01-08 文章作者:未知 信息來(lái)源:網(wǎng)絡(luò) 閱讀次數(shù):

八. 綜合應(yīng)用

  最后一個(gè)例子演示JavaScript對(duì)象的重要性。首先設(shè)置好一個(gè) Calendar(日歷)對(duì)象,然后根據(jù)需要顯示任何一個(gè)月的月歷。執(zhí)行過(guò)程不復(fù)雜,只需要指定月和年為對(duì)象屬性,然后讓構(gòu)造器做其它事情即可:

<script language="JavaScript">
/*  Calendar object, calendar.js
   Usage:
   obj = new Calendar(mm, yyyy);
   created 15.Mar.2001

   copyright Melonfire, 2001. all rights reserved.
   http://www.melonfire.com/community/columns/trog/

   demonstration only - not meant for production enviroments!!
*/

// constructor
function Calendar(month, year)
{

// array of day names
this.days = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday");

// array of month names
this.months = new Array("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December");

// array of total days in each month
this.totalDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

// object properties - month and year
// correction for zero-based array index
this.month = month-1;
this.year = year;

// leap year correction
if (this.year % 4 == 0)
{
this.totalDays[1] = 29;
}

// temporary variable - used later
this.rowCount = 0;

// object method
this.display = display;

// automatically run method display() once object is initialized
this.display();
}

// function to display calendar
function display()
{

// create a Date object
// required to obtain basic date information
// get the first and last day of the month - boundary values for calendar
obj = new Date(this.year, this.month, 1);
this.firstDayOfMonth = obj.getDay();
obj.setDate(31);
this.lastDayOfMonth = obj.getDay();

// start table
document.write("<table border=0 cellpadding=2 cellspacing=5>");

// month display
document.write("<tr><td colspan=7 align=center><font face=Arial
size=-1><b>" + this.months[this.month] + " " + this.year +
"</b></font></td></tr>");

// day names
document.write("<tr>");
for (x=0; x<7; x++)
{

document.write("<td><font face=Arial size=-2>" +
this.days[x].substring(0,3) + "</font></td>") ;
}
document.write("</tr>");

// start displaying dates
// display blank spaces until the first day of the month
document.write("<tr>");
for (x=1; x<=this.firstDayOfMonth; x++)
{
// this comes in handy to find the end of each 7-day block
this.rowCount++;
document.write("<td><font face=Arial size=-2> </font></td>");
}

// counter to track the current date
this.dayCount=1;
while (this.dayCount <= this.totalDays[this.month])
{
// use this to find out when the 7-day block is complete and display a new row
if (this.rowCount % 7 == 0)
{
document.write("</tr>\n<tr>");
}

// print date
document.write("<td align=center><font face=Arial size=-1>" + this.dayCount
+ "</font></td>");
this.dayCount++;
this.rowCount++;
}
// end table
document.write("</tr></table>");
}

// eof
</script>

以下解釋一下上面代碼的工作過(guò)程:

  最開(kāi)始的幾行設(shè)置了包含月和日名的數(shù)組以及每個(gè)月中總的天數(shù)。用一個(gè)簡(jiǎn)單的方程式來(lái)判斷某年是否為閏年,如果是的話(huà)就對(duì)二月的總天數(shù)進(jìn)行相應(yīng)修改。然后控制就傳給了對(duì)象方法display(),它負(fù)責(zé)將日歷寫(xiě)到頁(yè)面上。

  使用了Date對(duì)象和一些臨時(shí)變量之后,就創(chuàng)建了一個(gè)表格并用這個(gè)月的日期來(lái)填充好。document.write()方法負(fù)責(zé)設(shè)置 <table>、<tr> 和 <td> 標(biāo)記,然后將日期信息打印到表格單元中。

  以下代碼說(shuō)明如何使用這個(gè)對(duì)象:

<html>
<head>
<script language="JavaScript" src="calendar.js"></script>
</head>

<body bgcolor="white">
<script> obj1 = new Calendar(2, 2005); </script>
<script> obj2 = new Calendar(7, 2001); </script>
</body>
</html>

  代碼運(yùn)行。

這就是全部了。我希望本文中的例子能讓你對(duì) JavaScript對(duì)象的用途有一個(gè)正確的評(píng)價(jià),也許還能為你在開(kāi)發(fā)中使用它們提供幾個(gè)有用的思路。 

溫馨提示:喜歡本站的話(huà),請(qǐng)收藏一下本站!

本類(lèi)教程下載

系統(tǒng)下載排行

網(wǎng)站地圖xml | 網(wǎng)站地圖html
主站蜘蛛池模板: 崇信县| 巴彦淖尔市| 满城县| 聂拉木县| 祁阳县| 蒙山县| 乐业县| 宝清县| 梅河口市| 启东市| 镇宁| 合肥市| 得荣县| 泽州县| 井研县| 莱阳市| 化州市| 彰武县| 化隆| 钦州市| 靖西县| 兴化市| 江山市| 古蔺县| 望城县| 许昌市| 舟曲县| 大连市| 平远县| 沂源县| 舞阳县| 卢湾区| 琼结县| 荣昌县| 新余市| 合川市| 铜梁县| 建水县| 牟定县| 乌拉特中旗| 铜陵市|