document.getElementById/Name/TagName
2016-09-12 11:04
471 查看
document.getElementById
1、getElementById作用:一般页面里ID是唯一的,用于准备定位一个元素
语法: document.getElementById(id)
参数:id :必选项为字符串(String)
返回值:对象; 返回相同id对象中的第一个,按在页面中出现的次序,如果无符合条件的对象,则返回 null
example:
document.getElementById("id1").value;
2、getElementsByName
作用:按元素的名称查找,返回一个同名元素的数组
语法: document.getElementsByName(name)
参数:name :必选项为字符串(String)
返回值:数组对象; 如果无符合条件的对象,则返回空数组,按在页面中出现的次序
注意:返回数组值为value属性的值,
如果某标签无value属性,当你添加上value属性并赋值后,getElementsByName也能取到其值,
当未对value属性赋值时, getElementsByName返回数组值将是undefined ,
但仍能获得相同name标签的个数document.getElementsByName(name).length
当未设置name属性时document.getElementsByName仍能使用,它将根据你id取得value属性的值
example:
document.getElementsByName("name1")[0].value;
document.getElementsByName("name1")[1].value;
<span id='CBylawIndexName' class='normalNode' value='all' >全部</span>"
<span id='CBylawIndexName' class='normalNode' value='ALL' >全部</span>"
span标签其实没有name和value属性
但document.getElementsByName("CBylawIndexName")仍将取得value的值
3、getElementsByTagName
作用:按HTML标签名查询,返回一个相同标签元素的数组
语法: object.getElementsByTagName(tagname) object可以是document或event.srcElement.parentElement等
参数:tagname:必选项为字符串(String),根据HTML标签检索。
返回值:数组对象; 如果无符合条件的对象,则返回空数组,按在页面中出现的次序
example:
document.getElementsByTagName("p")[0].childNodes[0].nodeValue;
document.getElementsByTagName("p")[1].childNodes[0].nodeValue
相关文章推荐
- SpringMVC学习系列(8) 之 国际化
- rsync+inotify实现实时同步更新
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- HDU:Integer Inquiry
- 正则表达式(regex) 贪婪模式、懒惰模式
- Struts2的拦截器
- SpringMVC学习系列(7) 之 格式化显示
- man 5 proc
- 线程与进程的区别
- 批量执行redis命令
- app渲染性能相对专项测试理论基础
- Xshell选中文字自动发送Ctrl+C的问题
- Android中ADB用法总结
- SpringMVC学习系列(6) 之 数据验证
- Android-使用JitPack快速生成GitHub项目依赖地址
- SpringMVC3+Mybatis3(登录及CRUD操作)【转】
- 使用Jsoup采集拉勾网招聘信息
- cakephp recursive -1,0,1,2 速查
- KVM安装与配置
- Glide踩坑