JS-對象、瀏覽器對象

事件對象(Event)
    在觸發DOM事件的時候會產生一個對象
    1.type:                 獲取事件類型
    2.target:               獲取時間目標
    3.stopPropagation():    阻止事件冒泡
    4.preventDefault():     阻止事件默認行為
        <div id="div">
            <button id="btn1" onclick="demo()">按鈕</button>
        </div>
        <script>
            document.getElementById("btn1").addEventListener("click",showType);
            document.getElementById("div").addEventListener("click",showDiv);
            function showType(event){
                //alert(event.type);
                alert(event.target);
                event.stopPropagation();//阻止事件冒泡
            }
            function showDiv(){
                alert("div");
            }
        </script>

瀏覽器對象#

1.window對象######
        window.innerWidth  瀏覽器窗口的內部寬度 不包含進度條
        window.innerHeight 瀏覽器窗口的內部高度 不包含工具欄
        window.open()   打開新窗口
            window.open("index.html","標題","大小");
        window.close()  關閉當前窗口
2.計時器
        方法:
        setInterval()間隔指定的毫秒數不停地執行指定的代碼
        clearInterval()用于停止setInterval()方法執行的函數代碼
        <button id="btn" onclick="stopTime()">按鈕</button>
        <p id="ptime"></p>
        <script>
            var myTime = setInterval(function(){
                getTime();
            },1000);
            function getTime(){
                var d = new Date();
                var t = d.toLocaleTimeString();
                document.getElementById("ptime").innerHTML = t;
            }
            function stopTime(){
                clearInterval(myTime);
            }
        </script>
        setTimeout()暫停指定的毫秒數后執行的代碼
        clearTimeout()用于停止執行setTimeout()方法的函數代碼
3.History對象
        window.history 對象包含瀏覽器的歷史(url)的集合
        方法:
            history.back()      與在瀏覽器點擊后退按鈕相同
            history.forward()   與在瀏覽器中點擊向前按鈕相同
            history.go()        進入歷史中的某個頁面
        EXP:
            <button id="btn" onclick="goceshi()">按鈕</button>
            <script>
                function goceshi(){
                    history.back();
                }
            </script>
4.Location對象######
        window.location 對象用于獲得當前頁面的地址(url),并把瀏覽器重定向到新的頁面
        對象屬性:
            location.hostname       返回web主機的域名
            location.pathname       返回當前頁面的路徑和文件名
            location.port           返回web主機的端口
            location.protocol       返回所使用的web協議(http://或https://)
            location.href           返回當前頁面的URL
            location.assign()       加載新的文檔
            window.location.hostname;
            location.assign(url);
5.Screen對象
        window.screen 對象包含有關用戶屏幕的信息
        屬性:
            screen.availWidth       可用的屏幕寬度
            screen.availHeight      可用的屏幕高度
            screen.Height           屏幕高度
            screen.Width            屏幕寬度
            <script>
                document.write("可用高度:"+screen.availHeight+",可用寬度:"+screen.availWidth);
                document.write("高度:"+screen.height+",寬度:"+screen.width);
            </script>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • JS Window-瀏覽器對象模型 瀏覽器對象模型(BOM)使JS有能力與瀏覽器對話 由于現代瀏覽器幾乎實現了JS...
    figure_ai閱讀 1,296評論 0 2
  • 1.1 window對象是BOM的核心,window對象指當前的瀏覽器窗口。 window對象方法: window...
    jasmine_jing閱讀 419評論 0 0
  • 第1章 認識JS JavaScript能做什么?1.增強頁面動態效果(如:下拉菜單、圖片輪播、信息滾動等)2.實現...
    mo默22閱讀 1,326評論 0 5
  • 一、JS前言 (1)認識JS 也許你已經了解HTML標記(也稱為結構),知道了CSS樣式(也稱為表示),會使用HT...
    凜0_0閱讀 2,798評論 0 8
  • 踏過漫長的求學之路,身在一個高考大省的一名考生,我深知這份心酸, 結束了三年的高中生涯,我們來到無比憧憬的大學...
    kxob閱讀 405評論 0 2