今天学习了DOM操作与实战案例
代码:
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>根据id选择元素</title> </head> <body> <ul id="lists"> <li id="item1">列表项01</li> <li>列表项02</li> <li id="item2">列表项03</li> <li>列表项04</li> <li id="item3">列表项05</li> </ul> <script> let item1=document.getElementById('item1'); let item2=document.getElementById('item2'); let item3=document.getElementById('item3'); item1.style.backgroundColor='yellow'; item2.style.backgroundColor='yellow'; item3.style.backgroundColor='yellow'; function getElements() { let elements={}; for(let i=0;i<arguments.length;i++){ let id=arguments[i]; let elt=document.getElementById(id); if(elt===null){ throw new Error("No element with id: "+id); } elements[id]=elt; } return elements; } let elements=getElements('item1','item2','item3'); for(let key in elements){ elements[key].style.backgroundColor='coral'; } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>根据标签名Tag来获取元素<</title> </head> <body> <ul> <li>列表项01</li> <li>列表项02</li> <li>列表项03</li> <li>列表项04</li> <li>列表项05</li> </ul> <script> let ul=document.getElementsByTagName('ul')[0]; ul.style.backgroundColor='lightgreen'; //let ul1=document.getElementsByTagName('ul').item(0); //ul1.style.backgroundColor='lightblue'; let lists=document.getElementsByTagName('li'); console.log(lists); for(let i=0;i<lists.length;i++){ console.log(lists); for(let i=0;i<lists.length;i++){ lists[i].style.backgroundColor='lightpink'; } let ul2=document.getElementsByTagName('ul').item(0); let item2=ul2.getElementsByTagName('li').item(1); item2.style.backgroundColor='green'; } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>根据标签名Tag来获取元素<</title> </head> <body> <ul> <li>列表项01</li> <li>列表项02</li> <li>列表项03</li> <li>列表项04</li> <li>列表项05</li> </ul> <script> let ul=document.getElementsByTagName('ul')[0]; ul.style.backgroundColor='lightgreen'; //let ul1=document.getElementsByTagName('ul').item(0); //ul1.style.backgroundColor='lightblue'; let lists=document.getElementsByTagName('li'); console.log(lists); for(let i=0;i<lists.length;i++){ console.log(lists); for(let i=0;i<lists.length;i++){ lists[i].style.backgroundColor='lightpink'; } let ul2=document.getElementsByTagName('ul').item(0); let item2=ul2.getElementsByTagName('li').item(1); item2.style.backgroundColor='green'; } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>根据标签名Tag来获取元素<</title> </head> <body> <ul> <li>列表项01</li> <li>列表项02</li> <li>列表项03</li> <li>列表项04</li> <li>列表项05</li> </ul> <script> let ul=document.getElementsByTagName('ul')[0]; ul.style.backgroundColor='lightgreen'; //let ul1=document.getElementsByTagName('ul').item(0); //ul1.style.backgroundColor='lightblue'; let lists=document.getElementsByTagName('li'); console.log(lists); for(let i=0;i<lists.length;i++){ console.log(lists); for(let i=0;i<lists.length;i++){ lists[i].style.backgroundColor='lightpink'; } let ul2=document.getElementsByTagName('ul').item(0); let item2=ul2.getElementsByTagName('li').item(1); item2.style.backgroundColor='green'; } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>通过class属性选取元素</title> </head> <body> <ul class="ul"> <li class="red">列表项01</li> <li>列表项02</li> <li class="green">列表项03</li> <li>列表项04</li> <li class="coral large">列表项05</li> </ul> <script> let red=document.getElementsByClassName('red'); console.log(red); red[0].style.backgroundColor='red'; document.getElementsByClassName('ul').item(0).getElementsByClassName('green').item(0).style.backgroundColor='green'; let large=document.getElementsByClassName('coral large')[0]; large.style.backgroundColor='coral'; large.style.fontSize='1.5rem'; </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>使用css选择器来获取元素</title> </head> <body> <ul id="ul"> <li class="red">列表项01</li> <li>列表项02</li> <li class="green">列表项03</li> <li class="green">列表项04</li> <li class="coral large">列表项05</li> </ul> <script> let lists=document.querySelectorAll('li'); console.log(lists); let ul=document.querySelector('#ul'); console.log(ul); let li=ul.querySelectorAll('.green'); for(let i=0;i<li.length;i++){ li[i].style.backgroundColor='green'; } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>模拟智能在线kefu</title> <style> div:nth-child(1){ width:450px; height: 650px; background-color:lightblue; margin:30px auto; color:#333; box-shadow: 2px 2px 2px #808080; } h2{ text-align: center; margin-bottom:-10px; } div:nth-child(2){ width:400px; height: 500px; border:4px double green; background-color: #efefef; margin:20px auto 10px; } ul{ list-style: none; line-height:2em; overflow: hidden; padding: 15px; } table{ width:90%; height: 80px; margin: auto; } textarea{ border: none; resize: none; background: lightyellow; } button{ width: 60px; height:40px; background-color: seagreen; color:white; border:none; } button:hover{ cursor: pointer; background-color: orange; } </style> </head> <body> <div> <h2>在线kefu</h2> <div> <ul> <li></li> </ul> </div> <table> <tr> <td align="right"><textarea name="text" id="" cols="50" rows="4"></textarea></td> <td align="left"><button type="button">发现</button></td> </tr> </table> </div> <script> let btn=document.getElementsByTagName('button')[0]; let text=document.getElementsByName('text')[0]; let list=document.getElementsByTagName('ul')[0]; let sum=0; btn.onclick=function () { if(text.value.length===0){ alert('客官:是不是忘记输入内容了~~'); return false; } let userComment=text.value; text.value=''; let li=document.createElement('li'); li.innerHTML=userComment; let userPic='<img src="inc/peter.jpg" width="30" style="border-radius: 50%">'; li.innerHTML=userPic+' '+userComment; list.appendChild(li); sum+=1; setTimeout(function () { let info = [ '真烦人,有话快说,别耽误我玩抖音', '除了退货,退款,咱们什么都可以聊', '说啥,本姑娘怎么听不懂呢?再说一遍', '在我方便的时候再回复你吧~~', '投诉我的人多了,你算老几~~~' ]; let temp = info[Math.floor(Math.random()*3)]; //取1-5之间的一个整数:Math.floor(Math.random()*6 + 1) let reply = document.createElement('li'); let kefuPic = '<img src="inc/zly.jpg" width="30" style="border-radius:50%;">'; reply.innerHTML = kefuPic + ' ' + '<span style="color:red">'+temp+'</span>'; list.appendChild(reply); sum += 1; },2000); if (sum > 10) { list.innerHTML = ''; sum = 0; } } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例