# 直接獲取標簽 document.getElementById('gundong') #獲取id為gundong的元素 document.getElementsByClassName('qalist') #根據class屬性獲取元素數組 document.getElementsByTagName('ul') #根據標簽名獲取元素數組 document.getElementsByName('q') #根據name屬性獲取元素數組 # 間接獲取標簽 var tmp = document.getElementById('gundong') tmp.parentElement #獲取父節點元素 tmp.children # 獲取所有子元素 tmp.firstElementChild #第一個子元素 tmp.lastElementChild #最后一個子元素 tmp.nextElementSibling #下一個兄弟元素 tmp.previousElementSibling #上一個兄弟元素 #文本內容操作 tmp.innerText #獲取標簽內文本內容 tmp.innerText = '新的文本內容' #修改標簽內文本 tmp.innerHTML #獲取標簽中的所有內容,包含HTML代碼 tmp.innerHTML = '<a href="http:www.imdsx.cn">大師兄</a>' # innerHTML 可以將含有HTML代碼的字符串變為標簽 # input textarea標簽 tmp.value #獲取value屬性的值 tmp.value='新的內容' #對input textarea的value屬性重新賦值 #select標簽 tmp.value #獲取select標簽的value的值 tmp.value = '選項' # 修改select標簽的value的值 tmp.selectedIndex # 獲取select標簽的選項下標 #操作樣式 tmp.style.backgroundColor = 'red' #修改屬性 將背景色改為紅色 tmp.style.height = '100px' #修改屬性 修改高度 tmp.style.display='none' #修改屬性 隱藏 tmp.style.display='block' #修改屬性 改為可見 tmp.checked #獲取CheckBox的狀態 TRUE為勾選 #操作屬性 tmp.setAttribute('name','hhh') #增加一個name屬性,name='hhh' tmp.removeAttribute('placeholder') #刪除屬性 刪除placeholder屬性 location.href #獲取當前頁面url location.href='https://www.baidu.com' #跳轉到百度頁面 location.reload() #刷新 location.href=location.href #刷新
浙公網安備 33010602011771號