任務
1、新窗口打開時彈出確認框,是否打開
提示: 使用 if 判斷確認框是否點擊了確定,如點擊彈出輸入對話框,否則沒有任何操作。
2、通過輸入對話框,確定打開的網址,默認爲 http://www.imooc.com/
3、打開的窗口要求,寬400像素,高500像素,無菜單欄、無工具欄。
實現代碼:<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
<script type="text/javascript">
function openWindow()
{var open=confirm("確認新建窗口打開網站嗎?");
if(open==true)
// 新窗口打開時彈出確認框,是否打開
{var url=prompt("通過輸入對話框,確定打開的網址","http://www.imooc.com");
if(url!=null)
// 通過輸入對話框,確定打開的網址,默認爲 http://www.imooc.com/
{window.open(url,"_blank",'width=400px,height=500px,menubar=no,toolbar=no');
}
//打開的窗口要求,寬400像素,高500像素,無菜單欄、無工具欄。
else
{alert("再見!");}
}
else
{alert("再見!");}
}
任務
一、定義"改變顏色"的函數
提示:
obj.style.color
obj.style.backgroundColor
二、定義"改變寬高"的函數
提示:
obj.style.width
obj.style.height
三、定義"隱藏內容"的函數
提示: obj.style.display="none";
四、定義"顯示內容"的函數
提示: obj.style.display="block";
五、定義"取消設置"的函數
提示: 使用confirm()確定框,來確認是否取消設置。 如是將以上所有的設置恢復原始值,否則不做操作。
六、當點擊相應按鈕,執行相應操作,爲按鈕添加相應事件
<html>
<head>
<meta http-equiv="txttent-Type" txttent="text/html; charset=utf-8" />
<title>javascript</title>
<style type="text/css">
body{font-size:12px;}
#txt{
height:400px;
width:600px;
border:#333 solid 1px;
padding:5px;}
p{
line-height:18px;
text-indent:2em;}
</style>
</head>
<body>
<h2 id="con">JavaScript課程</H2>
<div id="txt">
<h5>JavaScript爲網頁添加動態效果並實現與用戶交互的功能。</h5>
<p>1. JavaScript入門篇,讓不懂JS的你,快速瞭解JS。</p>
<p>2. JavaScript進階篇,讓你掌握JS的基礎語法、函數、數組、事件、內置對象、BOM瀏覽器、DOM操作。</p>
<p>3. 學完以上兩門基礎課後,在深入學習JavaScript的變量作用域、事件、對象、運動、cookie、正則表達式、ajax等課程。</p>
</div>
<form>
<!--當點擊相應按鈕,執行相應操作,爲按鈕添加相應事件-->
<input type="button" value="改變顏色" onclick="set.changeColor()">
<input type="button" value="改變寬高" onclick="set.changeSize()">
<input type="button" value="隱藏內容" onclick="set.objHide()">
<input type="button" value="顯示內容" onclick="set.objShow()">
<input type="button" value="取消設置" onclick="set.offSet()">
</form>
<script type="text/javascript">
var txt=document.getElementById("txt");
var set={
changeColor:function(){
txt.style.color="red";
txt.style.backgroundColor="#ccc";
},
changeSize:function(){
txt.style.width="300px";
txt.style.height="300px";
},
objHide:function(){
txt.style.display="none";
},
objShow:function(){
txt.style.display="block";
},
offSet:function(){
var message=confirm("你確定要重置所有設置麼?");
if(message==true){
txt.removeAttribute('style');
}
}
}
</script>
</body>
</html>
任務
第一步: 創建構建運算函數count()。
第二步: 獲取兩個輸入框中的值和獲取選擇框的值。
提示:document.getElementById( id名 ).value 獲取或設置 id名的值。
第三步: 獲取通過下拉框來選擇的值來改變加減乘除的運算法則。
提示:使用switch判斷運算法則。
第四步: 通過 = 按鈕來調用創建的函數,得到結果。
注意: 使用parseInt()函數可解析一個字符串,並返回一個整數。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="txttent-Type" txttent="text/html; charset=utf-8" />
<title>javascript</title>
<style type="text/css">
body{font-size:12px;}
#txt{
height:400px;
width:600px;
border:#333 solid 1px;
padding:5px;}
p{
line-height:18px;
text-indent:2em;}
</style>
</head>
<body>
<h2 id="con">JavaScript課程</H2>
<div id="txt">
<h5>JavaScript爲網頁添加動態效果並實現與用戶交互的功能。</h5>
<p>1. JavaScript入門篇,讓不懂JS的你,快速瞭解JS。</p>
<p>2. JavaScript進階篇,讓你掌握JS的基礎語法、函數、數組、事件、內置對象、BOM瀏覽器、DOM操作。</p>
<p>3. 學完以上兩門基礎課後,在深入學習JavaScript的變量作用域、事件、對象、運動、cookie、正則表達式、ajax等課程。</p>
</div>
<form>
<!--當點擊相應按鈕,執行相應操作,爲按鈕添加相應事件-->
<input type="button" value="改變顏色" onclick="set.changeColor()">
<input type="button" value="改變寬高" onclick="set.changeSize()">
<input type="button" value="隱藏內容" onclick="set.objHide()">
<input type="button" value="顯示內容" onclick="set.objShow()">
<input type="button" value="取消設置" onclick="set.offSet()">
</form>
<script type="text/javascript">
var txt=document.getElementById("txt");
var set={
changeColor:function(){
txt.style.color="red";
txt.style.backgroundColor="#ccc";
},
changeSize:function(){
txt.style.width="300px";
txt.style.height="300px";
},
objHide:function(){
txt.style.display="none";
},
objShow:function(){
txt.style.display="block";
},
offSet:function(){
var message=confirm("你確定要重置所有設置麼?");
if(message==true){
txt.removeAttribute('style');
}
}
}
</script>
</body>
</html>
任務
第一步:可通過javascript的日期對象來得到當前的日期。
提示:使用Date()日期對象,注意星期返回值爲0-6,所以要轉成文字"星期X"
第二步:一長竄的字符串不好弄,找規律後分割放到數組裏更好操作哦。
第三步:分割字符串得到分數,然後求和取整。
提示:parseInt() 字符串類型轉成整型。
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>繫好安全帶,準備啓航</title>
<script type="text/javascript">
//通過javascript的日期對象來得到當前的日期,並輸出。
var currentDate = new Date();
var weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
document.write(
currentDate.getFullYear() + "年" +
(currentDate.getMonth() + 1) + "月" +
currentDate.getDate() + "日" + " " +
weekday[currentDate.getDay()]
);
//成績是一長竄的字符串不好處理,找規律後分割放到數組裏更好操作哦
var sorceStr = "小明:87;小花:81;小紅:97;小天:76;小張:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var sourceArr = sorceStr.split(";");
//document.write(sourceArr);//test
//從數組中將成績撮出來,然後求和取整,並輸出。
var score = new Array();
var sum = 0;
for (var i = 0; i < sourceArr.length; i ++) {
sum += parseInt(sourceArr[i].substr(sourceArr[i].indexOf(":")+1));
//document.write("<br>" + sum);//test
}
document.write("--班級總分爲:" + sum);
document.write(",班級平均分爲(保留整數):" + Math.round(sum/sourceArr.length))
</script>
</head>
<body>
</body>
</html>
任務
第一步: 先編寫好網頁佈局,如下:
第二步: 獲取顯示秒數的元素,通過定時器來更改秒數。
第三步: 通過window的location和history對象來控制網頁的跳轉。
實現代碼:<!DOCTYPE html>
<html>
<head>
<title>瀏覽器對象</title>
<meta http-equiv="Content-Type" content="text/html; charset=gkb" />
<style type="text/css">
</style>
</head>
<body>
<h4>操作成功</h4>
<p><span id="time">5</span>秒後回到主頁<a href="javascript:history.back()">返回</a></p>
<script type="text/javascript">
var num = document.getElementById("time").innerHTML
var i = setInterval("if(num > 1){document.getElementById('time').innerHTML = --num;}else{location.assign('http://www.imooc.com');}", 1000);
</script>
</body>