1. 程式人生 > >使用js內建物件document對html的標籤做一些動態的操作

使用js內建物件document對html的標籤做一些動態的操作

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js內建物件</title>
    <script>


//        第一大作用
//        1.1所有的全域性變數都是windows的屬性
//        1.2全域性的函式都是Windows的函式
        var age = 12;
        console.log(window.age);
//        全域性的函式
        function
Dog() {
console.log('這是一個可愛的方法'); } Dog(); window.Dog()
</script> </head> <body> </body> </html>





Document

<script>
    //documnenet內建物件的作用
    //動態獲取網頁中標籤節點
    //對獲取到內容,進行增刪改查

// 利用document寫一些東西
document.write(‘你好世界!’);
document.write(‘’);
document.write(‘

’)

</script>

比如實現一個標籤顯示與隱藏

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>圖片的影藏</title>




</head>
<body>
    <img class="icon" src="img/dkls.png">
    <p id="word">這裡的風景很美</p>
    <p></p
>
<button>隱藏</button> <script> //拿到所有操作的標籤; var icon = document.getElementsByClassName('icon')[0]; var p = document.getElementById('word'); var btn = document.getElementsByTagName('button')[0]; // 監聽按鈕的點選 console.log(btn); btn.onclick = function () { // 影藏css屬性 if(btn.innerText =='隱藏'){ p.style.display = 'none'; icon.style.display = 'none'; btn.innerText = '顯示'; }else { p.style.display = 'block'; icon.style.display = 'inline-block'; btn.innerText = '隱藏'; } } </script> </body> </html>