事半功倍之Javascript(3)

來源:互聯網
上載者:User
javascript

第八章 使用視窗

1.在瀏覽器的狀態列上顯示文本

<body onload=window.status="歡迎光臨我的網站">
<a href=http://www.sohu.com>sohu</a>
</body>

2.改變背景色

<script>
document.bgColor="orange"
</script>

3.列舉背景顏色

<body bgColor =green>
<script>
document.write("當前背景色是:"+document.bgColor)
</script>
</body>

4.改變文本和連結顏色

<script>
document.bgColor="orange"
document.fgColor="blue"
document.linkColor="red"
</script>
<h2>看看這段文本顏色</h2>
<a href=http://www.sohu.com>sohu</a>
</body>

5.改變文檔標題

<script>
name="Mouse"
document.title="welcome to "+name+"'s House"
document.write(document.title)
</script>

6.顯示修改日期

<script>
document.write("本頁面最後修改時間是"+document.lastModified)
</script>

7.查看當前文檔的URL

<script>
document.write("本頁面的URL:"+document.URL)
</script>

8.查看引用頁

<script>
document.write("本頁面的引用頁是"+document.referrer)
</script>

9.開啟新的瀏覽器視窗

<script>
window.open("*.htm","title","width=200,height=400,resizable=yes")
</script>

10.關閉遠程視窗

close.html:
<script>
document.write("本文")
</script>
<form name=form1>
<input type=button name=button1value="關閉" onclick=window.close()>
</form>

open.html
<script>
window.open("close.html","romote","width=200,height=400,resizable=yes")
</script>

11.列印視窗

<script>
document.write("本文")
</script>
<form name=form1>
<input type=button value=列印 onclick=window.print()>
</form>

12.移動視窗

<form name=form1>
水平方向<input type=text name=x value=20>
垂直方向<input type=text name=y value=50>
<input type=button value="移動視窗到…"onclick=window.moveTo(document.form1.x.value,document.form1.y.value)>
</form>

<form name=form1>
水平方向<input type=text name=x value=20>
垂直方向<input type=text name=y value=50>
<input type=button value="移動視窗"onclick=window.moveBy(document.form1.x.value,document.form1.y.value)>
</form>

13.改變視窗大小

<form name=form1>
水平方向<input type=text name=x value=200>
垂直方向<input type=text name=y value=500>
<input type=button value="改變視窗大小到….."onclick=window.resizeTo(document.form1.x.value,document.form1.y.value)>
</form>

<form name=form1>
水平方向<input type=text name=x value=200>
垂直方向<input type=text name=y value=500>
<input type=button value="改變視窗大小"onclick=window.resizeBy(document.form1.x.value,document.form1.y.value)>
</form>

14.用警告對話方塊通知使用者

<script>
window.alert("welcome")
</script>

15.用提示對話方塊接受輸入

<script>
name=window.prompt("輸入姓名","姓名")
document.write(" 歡迎您:"+name+"來到這裡")
</script>

16.用確認對話方塊使使用者做出決定

<script>
like=window.confirm("你覺得好嗎?")
if(like==true)
document.write("謝謝你的誇獎")
else
document.write("希望得到你的誇獎")
</script>

第九章 使用字串

1.使用字串對象

<script>
mystring="gdgdfgfddddaaaaaaaaaaaabbbbbbbbbbbbbbbbbvbhg.<br>"
document.write(mystring)
document.write(mystring.bold())
document.write(mystring.toUpperCase())
</script>

2.使用子字串

<script>
str1="fdsf 1111 gfdgfd dfdsf cccc dddd.<br>"
document.write(str1)
document.write(str1.substring(0,13)+"<br>")
document.write(str1.substr (20,11)+"<br>")
</script>

3.連接字串

<script>
str1="may you find"
str2="peace,happiness and prosperity.<br>"
document.write(str1+"<br>")
document.write(str2)
document.write(str1.concat(str2))
document.write(str1+=str2)
</script>

4.格式化字串變數

<script>
str1="peace,happiness and prosperity.<br>"
document.write(str1)
document.write(str1.big())
document.write(str1.small())
document.write(str1.bold())
document.write(str1.italics())
document.write(str1.strike())
document.write(str1.fontsize(6))
document.write(str1.fontcolor(green))
</script>

5.建立錨和連結

<script>
str1="this is the bigginning of the page.<br>"
str2="….<br>"
str3="this is the end of the page .<br>"
str4="link to the start<br>"
str5="link to the end<br>"
document.write(str1.anchor("start"))
for(i=0;i<10;i++)
document.write(str2);
document.write(str3.anchor("end"))
document.write(str4.link("#start"))
document.write(str5.link("#end"))
</script>

6.確定字串長度

<script>
str1="this is the bigginning of the page."
document.write(str1+"<br>")
document.write( "字串的長度是:"+str1.length)
document.write("字串全部大寫是;"+str1.toUpperCase())
document.write("字串全部小寫是;"+str1.toLowerCase())
</script>

7.在字串內搜尋

<script>
str1="this is the end of the line.<br>"
document.write(str1)
document.write("字元end在字串的位置是"+str1.search("end"))
document.write("字元dog在字串的位置是"+str1.search("dog"))
</script>

8.定位字串中的字元

<script>
str1="spring is a time for flowers and trees and baby bunnles<br>"
document.write(str1)
document.write("the index for the second word ‘and' is"+str1.indexOf("and",30))
documednt.write("the last index of the word ‘and' is "+str1.lastIndexOf("and"))
</script>

9.替換字串中的文本

<script>
str1="spring is a time for flowers and trees and baby bunnles<br>"
document.write(str1)
document .write(str1.replace("and",","))
</script>

10.字串分離

<script>
str1="spring is a time for flowers and trees and baby bunnles<br>"
document.write(str1)
str1array=str1.split(" ")
document.write(str1array[0]+"<br>")
document.write(str1array[1]+"<br>")
document.write(str1array[2]+"<br>")
document.write(str1array[3]+"<br>")
</script>

第十章 使用日期和時間

1.使用Date對象

<script>
cdate=new Date("august 2,1989 12:30:00")
document.write(cdate)
</script>

2.顯示當地時間和日期

<script>
cdate=new Date()
document.write("目前時間是:"+cdate.toGMTString()+"<br>")
document.write("日期和時間是:"+cdate.toLocaleString())
</script>

3.獲得時間和日期值

<script>
cdate=new Date()
document.write("顯示當前的星期"+cdate.getDay()+"<br>")
document.write("顯示當前的月份"+cdate.getMonth()+"<br>")
document.write("顯示當前的日期"+cdate.getDay()+"<br>")
document.write("顯示當前的年份"+cdate.getYear()+"<br>")
document.write("顯示當前的小時"+cdate.getHours()+"<br>")
document.write("顯示當前的分鐘"+cdate.getMinutes()+"<br>")
document.write("顯示當前的秒"+cdate.getSeconds()+"<br>")
</script>

4.設定時間和日期值

<script language=javascript>
cdate=new Date("December 25,1984")
document.write("顯示日期"+cdate+"<br>")
document.write("設定月份"+cdate.setMonth(10)+"<br>")
document.write("設定日期"+cdate.setDate(23)+"<br>")
document.write("設定年份"+cdate.setYear(2000)+"<br>")
document.write("設定小時"+cdate.setHours(13)+"<br>");
document.write("設定分鐘"+cdate.setMinutes(47)+"<br>");
document.write("設定秒"+cdate.setSeconds(23)+"<br>");
document.write("顯示設定後的日期和時間"+cdate);
</script>



相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。