Js-document操作

2023-03-12,,

# 直接获取标签
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 #刷新

Js-document操作的相关教程结束。

《Js-document操作.doc》

下载本文的Word格式文档,以方便收藏与打印。