JavaScript 對映像進行(追加,插入,替換,刪除)

來源:互聯網
上載者:User

標籤:charset   提示   containe   images   瀏覽器安全色   方式   css樣式   映像   onclick   

JavaScript 對映像進行(追加,插入,替換,刪除)本次所學內容:document.querySelector(‘.container‘)           這個是可以尋找單個【id標籤和class標籤】document.querySelectorAll(‘.container‘)       這個是可以尋找全部【li標籤和class標籤】container.appendChild(img)              這個是對container標籤進行追加子標籤container.firstElementChild            這個是在(Google瀏覽器安全色),功能是尋找container標籤下的第一個子標籤container.firstChild;                 這個是在(IE8相容),功能是尋找container標籤下的第一個子標籤container.lastElementChild                            這個是在(Google瀏覽器安全色),功能是尋找container標籤下的最後一個子標籤container.lastChild;                                         這個是在(IE8相容),功能是尋找container標籤下的最後 一個子標籤container.insertBefore(img,firstImg);               這個是對container標籤的子標籤進行插入功能,參數一個是要插入的標籤,後邊的參數是要在那個節點進行插入container.replaceChild(img,lastImg);            這個是對container標籤然後替換這個標籤下的標籤,共有倆個參數,一個是要替換的標籤,一個是被替換的標籤
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>1.節點方法:添加、替換、刪除</title><style>/*對class標籤進行css樣式*/.container{width:600px;/*設定寬度為600px*/height:600px;/*設定高度為600px*/border:1px solid #ddd;/*設定邊框 厚度為1px 使用實線,顏色為灰色*/}img{/*通過img標籤名進行尋找*/width:100px;/*設定寬度為100px*/height:100px;/*設定高度為100px*/border-radius:50%;/*設定映像顯示的範圍,50%為園型圖*/float:left;/*使用向左懸浮,去掉間隙*/}</style></head><body><div class="container"><!-- 定義一個div標籤 --><img src="images/1.jpg" /><!-- 在div標籤中定義一個img標籤 --></div><button>點擊追加一張圖片</button><!-- 定義一個按鈕標籤 --><button>點擊前面添加一張圖片</button><!-- 定義一個按鈕標籤 --><button>點擊替換最後一張圖片</button><!-- 定義一個按鈕標籤 --><button>點擊刪除第三張圖片(索引值為2)</button><!-- 定義一個按鈕標籤 --></body><script>/*方法// append:追加 child:子節點ul.appendChild(節點對象):// insert:添加 before:在...之前insertBefore(添加的節點,指定的節點)// replace:替換 replaceChild(新節點,被替換的節點)// remove:刪除removeChild(刪除的節點)*///1.擷取對象var container = document.querySelector(‘.container‘);// 擷取標籤並賦值到一個變數中var btn = document.querySelectorAll(‘button‘);// 擷取所有的標籤並賦值到一個變數中,擷取的是一個數組,每個數組中都是object對象//2.點擊事件,追加一張圖片images/2.jpgbtn[0].onclick = function(){// 通過下標索引擷取第一個button標籤,然後綁定一個點擊事件//(1)建立圖片節點var img = document.createElement(‘img‘);//建立一個新的img標籤,然後賦值給變數//(3)隨機數var index = rand(1,60);// 調用函數將函數的執行效果賦值給變數img.src = ‘images/‘ + index + ‘.jpg‘;// 使用拼接字元,然後設定標籤的src屬性img.alt = ‘美女圖‘;// 如果圖片沒有載入成功就就會返回美女圖字串// //console.log(img);//(2)追加圖片container.appendChild(img);// 這個是找到container 標籤然後在這個標籤的中添加字標籤}//在節點之前添加節點btn[1].onclick = function(){//建立圖片節點(添加一張新圖片,瀏覽器上沒有的圖片,就只能建立)var img = document.createElement(‘img‘);img.src = ‘images/‘ + rand(1,60) +‘.jpg‘;img.alt = ‘美女頭像圖‘;//firstElementChild(Google相容)//firstChild(IE8相容)var firstImg = container.firstElementChild || container.firstChild;//擷取第一張圖片//頭部添加圖片(在節點之前添加)container.insertBefore(img,firstImg);// 使用插入的方法共有倆個參數,一個是要插入的內容是什麼,一個參數是要在什麼節點之前,}//替換節點btn[2].onclick = function(){//1.新節點var img = document.createElement(‘img‘);// 建立一個新的標籤img.src = ‘images/‘+rand(1,60)+‘.jpg‘;// 使用字串拼接,然後賦值給一個變數img.alt = ‘加班中‘;// 如果圖片載入錯誤,就提示支付串加班中//2.最後一個節點var lastImg = container.lastElementChild || container.lastChild;// 尋找container標籤下的最後一個標籤,然後賦值到一個新的變數中//3.替換節點container.replaceChild(img,lastImg);// 找到container標籤然後替換這個標籤下的標籤,共有倆個參數,一個是要替換的標籤,一個是被替換的標籤}//點擊刪除第三張圖片btn[3].onclick = function(){//擷取第三張圖片var threeChild = container.children[2];//擷取了所有的標籤子節點//console.log(threeChild);//刪除該圖片container.removeChild(threeChild);}//點擊一次隨機出現一張(1-60張圖片)//隨機數函數function rand(m,n){// 定義一個函數,需要有倆個參數//隨機數return Math.floor(Math.random()*(n-m+1)+m);// 返迴向下取整的方式然後執行隨機數的函數}</script></html>

JavaScript 對映像進行(追加,插入,替換,刪除)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.