javascript基礎(chǔ)第一章 JavaScript與用戶端
更新時間:2010年07月22日 13:27:32 作者:
javascript基礎(chǔ)第一章 JavaScript與用戶端
一 頁面輸出
1.頭部文件
<head>
<script language="javascript">
document.write("腳本之家www.dbjr.com.cn");
</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.dbjr.com.cn" 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.dbjr.com.cn");
</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.dbjr.com.cn" 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)文章
在JavaScript應(yīng)用中實(shí)現(xiàn)延遲加載的方法
這篇文章主要介紹了在JavaScript應(yīng)用中實(shí)現(xiàn)延遲加載的方法,利用RequireJS框架,需要的朋友可以參考下2015-06-06詳細(xì)介紹8款超實(shí)用JavaScript框架
下面盤點(diǎn)了8款實(shí)用的JavaScript框架,有需要的朋友可以參考一下2013-10-10javascript?DOM?querySelectorAll()?使用方法
querySelectorAll()?方法返回文檔中匹配指定?CSS?選擇器的所有元素,返回?NodeList?對象,一般用來獲取指定id火class下的所有節(jié)點(diǎn)2023-06-06在JavaScript中操作數(shù)組之map()方法的使用
這篇文章主要介紹了在JavaScript中操作數(shù)組之map()方法的使用,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-06-06詳解JavaScript實(shí)現(xiàn)異步Ajax
本文詳細(xì)講解了JavaScript實(shí)現(xiàn)異步Ajax的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05