亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 7
粉絲 0
評(píng)論 0
訪問量 3447
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
演示元素查詢的api方法和演示元素遍歷與增刪改常用方法
P粉276126820
原創(chuàng)
441人瀏覽過

1、元素查詢和api方法

  1. <ul class="list">
  2. <li>item1</li>
  3. <li>item2</li>
  4. <li>item3</li>
  5. <li>item4</li>
  6. <li>item5</li>
  7. <li>item6</li>
  8. </ul>
  9. <script>
  10. // 獲取所有子節(jié)點(diǎn)
  11. const list =document.querySelector('.list')
  12. let items = list.childNodes//所有的的子節(jié)點(diǎn)(包括回車)
  13. items = [...items].filter(item=>item.nodeType==1)//獲取元素節(jié)點(diǎn)。
  14. // 另一種簡(jiǎn)便方法
  15. items=list.children
  16. // 獲取第一個(gè)子元素
  17. console.log(items[0].textContent)
  18. console.log(list.firstElementChild.textContent)
  19. //獲取最后一個(gè)子元素節(jié)點(diǎn)
  20. console.log(items[items.length-1].textContent)
  21. console.log(list.lastElementChild)
  22. //獲取其中一個(gè)字元素節(jié)點(diǎn)
  23. let four =document.querySelector('.list :nth-child(4)')
  24. console.log(four.textContent)
  25. // 獲取子節(jié)點(diǎn)的前一個(gè)
  26. console.log(four.previousElementSibling.textContent)
  27. //獲取子節(jié)點(diǎn)的后一個(gè)
  28. console.log(four.nextElementSibling.textContent)
  29. // 獲取子元素的父節(jié)點(diǎn)
  30. let parent = four.parentElement
  31. console.log(parent)
  32. console.log(parent.contains(four))
  33. // ———————————————————————指定位置插入—————————————————————
  34. // 2 API 方法
  35. // 1. `insertAdjacentElement()`:指定位置插入**元素**
  36. // 2. `insertAdjacentText()`: 指定位置插入**文本**節(jié)點(diǎn)
  37. // 3. `insertAdjacentHTML()`: 指定位置插入**元素節(jié)點(diǎn)**(DOMString)
  38. let h3 = document.createElement('h3')
  39. h3.textContent='hello world'
  40. // 1. `beforebegin`:元素自身的前面
  41. list.insertAdjacentElement('beforebegin',h3)
  42. // 2. `afterbegin`:插入元素內(nèi)部的第一個(gè)子節(jié)點(diǎn)之前
  43. list.insertAdjacentElement('afterbegin',h3)
  44. // 3. `beforeend`:插入元素內(nèi)部的最后一個(gè)子節(jié)點(diǎn)之后
  45. list.insertAdjacentElement('beforeend',h3)
  46. // 4. `afterend`:元素自身的后面
  47. list.insertAdjacentElement('afterend',h3)
  48. //追加元素
  49. list.insertAdjacentHTML('afterend','<button onclick="this.remove()">刪除</button>')
  50. </script>

2、元素遍歷與增刪改常用方法

  1. <script>
  2. //創(chuàng)建元素
  3. const ul =document.createElement('ul')
  4. // 文檔中添加元素
  5. document.body.append(ul)
  6. console.log('111')
  7. // 文檔片段方式添加(防止數(shù)據(jù)量大造成頁面抖動(dòng),用文檔片段的方法去做)
  8. const frag =document.createDocumentFragment('ul')
  9. for(let i=0;i<6;i++){
  10. const li = document.createElement('li')
  11. li.append('item-'+(i+1))
  12. frag.append(li)
  13. }//創(chuàng)建完后一次性添加到ul中。
  14. ul.append(frag)
  15. // after方法
  16. let three= ul.querySelector(':nth-child(3)')
  17. let li = document.createElement('li')
  18. li.append('指定子元素前面新增')
  19. three.before(li)
  20. //before
  21. li = document.createElement('li')
  22. li.append('指定子元素后面新增新增')
  23. three.before(li)
  24. //console.log(three)
  25. //克隆
  26. ul.append(li.cloneNode(true))
  27. // 替換/更新最后一個(gè)子元素
  28. let h2 = document.createElement('h2')
  29. h2.textContent='hello world'
  30. ul.replaceChild(h2,ul.lastElementChild)
  31. //刪除指定元素
  32. ul.querySelector(':nth-child(3)').remove()
  33. // ———————————————————————元素內(nèi)容添加—————————————————————
  34. //textContent可以獲取css 隱藏元素都可以獲取,innerText不能獲取css及隱藏元素
  35. console.log(document.querySelector('h2').textContent);
  36. console.log(document.querySelector('h2').innerText);
  37. //直接原樣輸出
  38. document.querySelector('.box').innerText='<h3 style="color: red;">測(cè)試innerHtml和innerText</h3>'
  39. //渲染后顯示
  40. document.querySelector('.box').innerHTML='<h3 style="color: red;">測(cè)試innerHtml和innerText</h3>'
  41. //替換自己
  42. document.querySelector('.box').outerHTML=null
  43. // 同remove等效
  44. // document.querySelector('.box').remove()
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)