Educoder JavaScript學習手冊:瀏覽器對象模型

第1關:定時器

設置執行一次的定時任務timerTask(),延遲爲2000毫秒,任務的唯一標識賦值給變量timeId。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p onclick="handleTimer()">set timer then undo</p>
    <script>
        var timeId;
        function timerTask() {
            console.log("this is timer task");
        }
        function handleTimer() {
			//請在此處編寫代碼
			/********** Begin **********/
            var timeId=window.setTimeout(timerTask,2000)
            
            
			/********** End **********/
        }
    </script>
</body>
</html>

第2關:循環定時器

取消定時任務timerTask1(),要求使用本關介紹的方法。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p onclick="task1()">this is task onea</p>
    <p onclick="task2()">this is task two</p>
    <p onclick="removeTask1()">try to remove task one</p>
    <script>
        var timeId1;
        var timeId2;
        function timerTask1() {
            console.log("timerTask1!");
        }
        function timerTask2() {
            console.log("timerTask2!");
        }
        function task1() {
            timeId1 = window.setInterval(timerTask1,2000);
        }
        function task2() {
            timeId2 = window.setInterval(timerTask2,1500);
        }
        function removeTask1() {
			//請在此處編寫代碼
			/********** Begin **********/
              window.clearInterval(timeId1);
             window.clearInterval(timeId2);
			/********** End **********/
        }
    </script>
</body>
</html>

第3關:location對象

第一步,獲取location對象,賦值給變量loc;
第二步,利用loc打印出當前頁面地址的主機名;
第三步,利用loc實現跳轉到https://www.educoder.net/forums/categories/all?order=newest;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p onclick="openNew()">Click me to new page!</p>
    <script>
        function openNew() {
		//請在此處編寫代碼
		/********** Begin **********/
        var loc=window.location;
        console.log("hostname:"+loc.hostname);
        window.location.href="https://www.educoder.net/forums/categories/all?order=newest"
        
        
		/********** End **********/
        }
    </script>
</body>
</html>

第4關:對話框

第一步:彈出一個輸入型對話框(輸入框),用戶的輸入結果賦值給變量result,輸入框提示語爲your name,輸入框默認值爲Xiao Ming;
第二步:使用result判斷用戶的輸入是否爲null(不是字符串null,是表示空的null),如果是,在控制檯打印name cannot be null

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p onclick="inputName()">Click to input name!</p>
    <script>
        function inputName() {
            var result;
			//請在此處編寫代碼
			/********** Begin **********/
            var result=window.prompt("your name","Xiao Ming");
            if(result==mull){
                console.log("name cannot be null")
            }
            
			/********** End **********/
        }
    </script>
</body>
</html>

第5關:窗口

打開一個窗口,它的文檔的地址是Demo.html,name屬性是窗口名字window_name,specs和replace不用設置。將打開的窗口賦值給變量myWindow

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p onclick="openNewWindow()">open new window</p>
    <script>
        var myWindow;
        function openNewWindow() {
		//請在此處編寫代碼
		/********** Begin **********/
        myWindow=window.open("Demo.html","window_name")
        
        
		/********** End **********/
        }
    </script>
</body>
</html>

覺得有幫助的同學點點關注嘻嘻!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章