JavaScript-21.內(nèi)置對(duì)象document

1.可以動(dòng)態(tài)獲取網(wǎng)頁中所有的標(biāo)簽(節(jié)點(diǎn))

2.可以對(duì)獲取到的標(biāo)簽進(jìn)行增刪改查CRUD

document.write('helloword');
document.write('<input type="file">');
document.write('![](https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png) ');

3. document的常用操作

  • 1.獲取網(wǎng)頁中的圖像標(biāo)簽,改變src的屬性

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>8.document的常用操作</title>
    <script>
         function changeImg() {
    
       // 1.獲取網(wǎng)頁中的圖像標(biāo)簽
       var image = document.getElementsByClassName('icon')[0];// 要string類型
       console.log(image);
       // 2.改變src的屬性
       image.src='js/image/icon_05.png';
    
      }
    
     </script>
    
    </head>
    <body>
    
    ![](js/image/icon_09.png)
    <p></p>
    <button onclick="changeImg();">更換圖片</button>
    </body>
    
    1. 對(duì)標(biāo)簽的操作,拿到所有要操作的標(biāo)簽,監(jiān)聽按鈕的點(diǎn)擊
  <!DOCTYPE html>
  <html lang="en">
  <head>
  <meta charset="UTF-8">
  <title>9.document的常用操作2</title>

  </head>
  <body>

     ![](js/image/icon_03.png)
     <p id="word">這里風(fēng)景很美</p>
      <p></p>
      <button>隱藏</button>
   <script>
    //1.拿到所有要操作的標(biāo)簽
    var icon = document.getElementsByClassName('icon')[0];
    var p = document.getElementById('word');
    var btn = document.getElementsByTagName('button')[0];

  //2.監(jiān)聽按鈕的點(diǎn)擊
  btn.onclick = function () {

      if(btn.innerText == '隱藏')
      {
          // 隱藏 css屬性 display

          p.style.display = 'none';
          icon.style.display = 'none';
          btn.innerText = '顯示';

      }else {

          // 隱藏 css屬性 display (還原本來的標(biāo)簽類型)

          p.style.display = 'block';
          icon.style.display = 'inline-block';
          btn.innerText = '隱藏';
      }
     }
  </script>
  </body>
</html>
顯示與隱藏

感悟:如果想改變一個(gè)標(biāo)簽,首先要拿到這個(gè)標(biāo)簽,讓后對(duì)其做出操作

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容