javascript基礎(chǔ)第一章 JavaScript與用戶端
更新時間:2010年07月22日 13:27:32 作者:
javascript基礎(chǔ)第一章 JavaScript與用戶端
一 頁面輸出
1.頭部文件
<head>
<script language="javascript">
document.write("腳本之家www.fzitv.net");
</script>
</head>
2.頁面內(nèi)
<body>
<script>
document.write("腳本之家");
</script>
</body>
3.外部文件
<script src="display.js"></script>
4.利用頁面ID的innerHtml
<script>
window.onload = writeMessage; // 頁面加載時調(diào)用writeMessage函數(shù)
writeMessage() {
document.getElementById("helloMessage").innerHTML = "腳本之家";
//找到dom ID(helloMessage),修改其html內(nèi)容
}
</script>
5.警告
alert("廣州百匯物流有限公司");
6.詢問
if (confirm("是否訪問我們的首頁"))
{
alert("是的,前往");
}
else {
alert("退出");
}
7.輸入
var ans = prompt("輸入你的留言","您好,");
if (ans) {
alert("你說:" + ans);
}
else {
alert("退出,沒有留言");
}
8.頁面跳轉(zhuǎn)
<script>
window.onload = initAll;
function initAll() {
document.getElementById("redirect").onclick = initRedirect;
}
function initRedirect()
{
window.location = "index.html";
return false;
}
</script>
<a href="http://www.fzitv.net" id="redirect">腳本之家</a>
9.判斷分支
<script>
window.onload = initAll;
function initAll() {
document.getElementById("Lincoln").onclick = saySomething;
document.getElementById("Kennedy").onclick = saySomething;
document.getElementById("Nixon").onclick = saySomething;
}
function saySomething() {
switch(this.id) {
case "Lincoln":
alert("Four score and seven years ago...");
break;
case "Kennedy":
alert("Ask not what your country can do for you...");
break;
case "Nixon":
alert("I am not a crook!");
break;
default:
}
}
</script>
<form action="#">
<input type="button" id="Lincoln" value="Lincoln" />
<input
type="button" id="Kennedy" value="Kennedy" />
<input type="button" id="Nixon"
value="Nixon" />
</form>
10.異常捕獲
window.onload = initAll;
function initAll() {
var ans = prompt("輸入?yún)?shù):","");
try {
if (!ans || isNaN(ans) || ans<0) {
throw new Error("輸入為非數(shù)");
}
alert("根號" + ans + " 是 " + Math.sqrt(ans));
}
catch (errMsg) {
alert(errMsg.message);
}
}
1.頭部文件
復(fù)制代碼 代碼如下:
<head>
<script language="javascript">
document.write("腳本之家www.fzitv.net");
</script>
</head>
2.頁面內(nèi)
復(fù)制代碼 代碼如下:
<body>
<script>
document.write("腳本之家");
</script>
</body>
3.外部文件
<script src="display.js"></script>
4.利用頁面ID的innerHtml
復(fù)制代碼 代碼如下:
<script>
window.onload = writeMessage; // 頁面加載時調(diào)用writeMessage函數(shù)
writeMessage() {
document.getElementById("helloMessage").innerHTML = "腳本之家";
//找到dom ID(helloMessage),修改其html內(nèi)容
}
</script>
5.警告
alert("廣州百匯物流有限公司");
6.詢問
復(fù)制代碼 代碼如下:
if (confirm("是否訪問我們的首頁"))
{
alert("是的,前往");
}
else {
alert("退出");
}
7.輸入
復(fù)制代碼 代碼如下:
var ans = prompt("輸入你的留言","您好,");
if (ans) {
alert("你說:" + ans);
}
else {
alert("退出,沒有留言");
}
8.頁面跳轉(zhuǎn)
復(fù)制代碼 代碼如下:
<script>
window.onload = initAll;
function initAll() {
document.getElementById("redirect").onclick = initRedirect;
}
function initRedirect()
{
window.location = "index.html";
return false;
}
</script>
<a href="http://www.fzitv.net" id="redirect">腳本之家</a>
9.判斷分支
復(fù)制代碼 代碼如下:
<script>
window.onload = initAll;
function initAll() {
document.getElementById("Lincoln").onclick = saySomething;
document.getElementById("Kennedy").onclick = saySomething;
document.getElementById("Nixon").onclick = saySomething;
}
function saySomething() {
switch(this.id) {
case "Lincoln":
alert("Four score and seven years ago...");
break;
case "Kennedy":
alert("Ask not what your country can do for you...");
break;
case "Nixon":
alert("I am not a crook!");
break;
default:
}
}
</script>
<form action="#">
<input type="button" id="Lincoln" value="Lincoln" />
<input
type="button" id="Kennedy" value="Kennedy" />
<input type="button" id="Nixon"
value="Nixon" />
</form>
10.異常捕獲
復(fù)制代碼 代碼如下:
window.onload = initAll;
function initAll() {
var ans = prompt("輸入?yún)?shù):","");
try {
if (!ans || isNaN(ans) || ans<0) {
throw new Error("輸入為非數(shù)");
}
alert("根號" + ans + " 是 " + Math.sqrt(ans));
}
catch (errMsg) {
alert(errMsg.message);
}
}
相關(guān)文章
關(guān)于javascript中的typeof和instanceof介紹
typeof用來檢測給定變量的數(shù)據(jù)類型 instanceof用來檢測對象的類型2012-12-12
Javascript入門學(xué)習(xí)第七篇 js dom實例操作
上篇文章我們講了 用dom方式 創(chuàng)建節(jié)點,復(fù)制節(jié)點,插入節(jié)點。 今天我們將講 刪除節(jié)點,替換節(jié)點,查找節(jié)點等。2008-07-07
Javascript實例教程(19) 使用HoTMetal(7)
Javascript實例教程(19) 使用HoTMetal(7)...2006-12-12
javascript setTimeout和setInterval 的區(qū)別
window對象有兩個主要的定時方法,分別是setTimeout 和 setInteval 他們的語法基本上相同,但是完成的功能取有區(qū)別。2009-12-12
ajax提交表單實現(xiàn)網(wǎng)頁無刷新注冊示例
這篇文章主要介紹了ajax提交表單實現(xiàn)網(wǎng)頁無刷新注冊示例,需要的朋友可以參考下2014-05-05
JavaScript中Date.toSource()方法的使用教程
這篇文章主要介紹了JavaScript中Date.toSource()方法的使用教程,用來返回日期為字符串,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-06-06

